开发者

reusable ajax editor for model in asp.net mvc not going ajax

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:

reusable ajax editor for model in asp.net mvc not going ajax

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.
        }开发者_StackOverflow中文版

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜