问题
I have a view:
@model X.Models.Employee
@Ajax.ActionLink(Model.LastName[lastNameCount - 1].Value1, // <-- Text to display
"Stuff", // <-- Action Method Name
new
{
//LINE CAUSING ISSUE
employee = Model
},
new AjaxOptions
{
bla
},
new
{
@id = "opener"
})
and my controller action method looks like this
ActionResult Stuff(Employee employee) {
//stuff
}
why is my employee returning null? can I not assign the Model to the object route values?
I've tried Model.property
and the action method accepts the type associated with that and it works, but not with just Model
回答1:
You using the wrong overload. Employee
is a complex object so replace the 3rd parameter
Ajax.ActionLink(...
Stuff",
new
{
employee = Model
},
new AjaxOptions
...
with
Ajax.ActionLink(...
Stuff",
Model,
new AjaxOptions
...
However Model.LastName[lastNameCount - 1].Value1
suggests property LastName
is a collection which wont work (route parameters cannot be created for collections and if you inspect the html you will see something like ...LastName=System.Collections.Generic.List...)
. In that case you will need to just pass the ID of the employee
Ajax.ActionLink(...
"Stuff",
new { ID = Model.ID },
new AjaxOptions
....
public ActionResult Stuff(int ID) {
来源:https://stackoverflow.com/questions/26348680/ajax-actionlink-object-route-values-returning-null