ok so Im new to mvc. What Im making is a -create user form-. The UserViewModel has a branchcode property of type long. I want a small branch selector, where instead of a plain textbox for the code I have a textbox and a check name link, when clicked would return the name of the branch with some ajax.

So I made a branch selector and put it under Shared/EditorTemplates. Looks like this

@model long

<div class="editor-label">
    Branch ID: @Html.TextBoxFor(model => model)
    @Ajax.ActionLink("Get Branch Name", MVC.Admin.Home.BranchDetail(Model),
        new AjaxOptions { UpdateTargetId = "BranchName", LoadingElementId = "loading" })
    <p id="loading" style="display: none">
        Loading...
    </p>
</div>
<div id="BranchName" class="editor-field">
</div>

I have the UIHint setup in the viewmodel so when I say EditFor(model.BranchName) it shows up perfectly like this: alt text

My problems:

1. There is no ajax! When I click it goes the the url : http://localhost:1159/Admin/Home/BranchDetail?brcd=0 And then the URL throws an error because there is no BranchDetailsView anywhere.

This is what the controller looks like:

public virtual ActionResult BranchDetail(long brcd)
        {
            if (Request.IsAjaxRequest())
            {
                //code to find branch
                if (br == null)
                {
                    return Content("No Branch Found");
                }
                else
                {
                    return Content(br.BranchName + "Circle: " + br.Circle);
                }
            }
            return View();//we should not be here.
        }

2. Another problem is, how do I get the AjaxLink to return what code what typed. If you notice the screenie, the url to the link has brcd=0. I want to send over the branch code to the controller! I tried BeginRouteForm with an input button inside the editor, but that ends up submitting the entire Create page form? Would that be the right way to do this?

Note: Im using T4MVC, also, Initially I forgot but I did reference both microsoft.ajax.js and microsoft.mvc.ajax.js in the create page.


Update : I managed to get this working with JQuery and it seems pretty nice and simple. However, I would really like to know how this kind of thing would be generally done with Ajax.BeginForm since Im learning MVC.

link|improve this question

feedback

1 Answer

Seems like you are using MVC 3, so you got to reference different scripts.

jquery-1.4.4.js
jquery.unobtrusive-ajax.js

to begin with

and it seems that there is no way to pass client side data to @Ajax.ActionLink So you might jump straight to implementing your lookup with a little help of jQuery.

link|improve this answer
Yes I do have jquery, jquery.validate.min, jquery.unobtrusive and the 2 ajax js files. – gideon Dec 24 '10 at 8:40
you need to reference them in your page for @Ajax.ActionLink to work – Alexander Taran Dec 24 '10 at 8:43
Yes yes! the _layout page has all the jquery files and the editor has the microsoft.ajax. What about using an ajax form? – gideon Dec 24 '10 at 8:47
microsoft.ajax should not be referenced.. it is there for legacy mvc2 projects. – Alexander Taran Dec 24 '10 at 9:18
So its just microsoft.mvc.ajax? Fortunately I got it to work with <3 jquery perfectly! but I'd like to get it working with Ajax.BeginForm() – gideon Dec 24 '10 at 9:21
show 2 more comments
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.