I have the following code in a simple MVC view:
<div id="CarText"><%=Model.Cars[10].Name %></div>
<div id="SelectedCar">no car selected</div>
<%=Ajax.ActionLink("ajax test","TestMethod",new {carObj = Model.Cars[10]},new AjaxOptions {UpdateTargetId = "SelectedCar"})%>
Then in my controller, I have the following:
public ActionResult TestMethod(Car carObj)
{
return PartialView("SelectedCar", carObj);
}
When I run the page, it all renders as I expect (eg, the name of the 11th car is displayed in the first div.
Then, when I click the link, the code in the controller is called, but the “carObj” parameter is always null.
I’ve read a fair few pages and blogs now and I’m failing to see what I’ve done wrong… hence this question.
Answers, as always, greatly appreciated 🙂
So it turns out you can’t pass an object via an ActionLink…
I suppose that makes sense when you think about it really.
Ho-hum.. guess I’m going to have to add an ID parameter simply so I can reference it via Ajax :/