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:
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.