0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Yes I do have jquery, jquery.validate.min, jquery.unobtrusive and the 2 ajax js files.
you need to reference them in your page for @Ajax.ActionLink to work
Yes yes! the _layout page has all the jquery files and the editor has the microsoft.ajax. What about using an ajax form?
microsoft.ajax should not be referenced.. it is there for legacy mvc2 projects.
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()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.