Ajax.actionlink object route values returning null

安稳与你 提交于 2019-12-25 01:25:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!