I am currently working on a MVC3 project. I currently stuck on this portion.
I have this Ajax form which contains several hyperlink
@using (Ajax.BeginForm("Click", new AjaxOptions { UpdateTargetId = "showpage" }))
{
<a href="#" id="0" onclick='document.forms[0].submit()'>Link 0</a>
<a href="#" id="1" onclick='document.forms[0].submit()'>Link 1</a>
}
I have this function called "Click" in a controller to fire whenever there'a link clicked.
public ActionResult Click(String id)
{
// Action here
}
However, Whenever I click one of the hyperlinks, the click function receive id as "null". I want the function to know which hyperlink is click when enter the controller. Help needed :(
Instead of hyperlinks you can use submit buttons with the
name
property.I would recommend you to use a separate forms per button (which contrary to classic WebForms is something that you should not be afraid of in ASP.NET MVC) and of course use submit buttons instead of hyperlink which are the semantically correct element to submit a form:
Now inside your
Click
action you will get the correct id.From my understanding, I think you're after something like this:
HTML:
SCRIPT:
CONTROLLER:
Hope this helps