Ajax.actionlink对象路径的值返回null [英] Ajax.actionlink object route values returning null

查看:209
本文介绍了Ajax.actionlink对象路径的值返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个观点:

@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
}

为什么我的员工返回null?我不能模型分配给对象路由值?

why is my employee returning null? can I not assign the Model to the object route values?

我试过 Model.property 和行动方法接受与该关联的类型和它的作品,而不是只用型号

I've tried Model.property and the action method accepts the type associated with that and it works, but not with just Model

推荐答案

您使用了错误的过载。 员工是一个复杂的对象,因此更换第三个参数

You using the wrong overload. Employee is a complex object so replace the 3rd parameter

Ajax.ActionLink(...
  Stuff",
  new
  {
    employee = Model
  },
  new AjaxOptions
  ...

Ajax.ActionLink(...
  Stuff",
  Model,
  new AjaxOptions
  ...

Model.LastName [lastNameCount - 1] .Value1 显示属性是不会工作的集合(路由参数不能为集合创建,如果你检查HTML,你会看到类似 ...姓氏= System.Collections.Generic.List ...)。在这种情况下,你需要你只需要把这个员工的ID

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) {

这篇关于Ajax.actionlink对象路径的值返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆