【问题标题】:ASP.NET MVC 4 Passing Object Variable Through ActionLinkASP.NET MVC 4 通过 ActionLink 传递对象变量
【发布时间】:2013-10-21 22:59:08
【问题描述】:

我有一个 ASP.NET MVC 4 应用程序。我的应用程序中有一个 Razor 视图,它使用 List 类型作为模型。

@model List<MeetingLog.Models.UserModel>

@{
    Layout = null;
}
.
.
.

我正在像这样迭代模型变量:

@foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @item.Name
                        </td>
                        <td>
                            @item.Surname
                        </td>
                        <td>
                            @Html.ActionLink("Click", "SavePerson", "Meeting", new {model = item, type = "coordinator"}, null)
                            @Html.ActionLink("Click", "SavePerson", "Meeting", new {model = item, type = "participant"}, null)
                        </td>
                    </tr>
                }

我需要通过操作链接将两个变量传递给 SavePerson 操作。第一个是当前用户模型,第二个是字符串变量命名类型。但是在我的操作中,第一个参数是 null。但是字符串参数正确。我怎样才能做到这一点?

【问题讨论】:

    标签: asp.net-mvc-4 razor html.actionlink


    【解决方案1】:

    当您对传递的值进行编码时,您实际上可以,只是有点奇怪(新{}) 您要做的就是将它作为您正在构建的新对象传递,因此它最终成为:

    @Html.ActionLink("Link Name", "LinkActionTarget", new Object{model = item, type ='Coordinator'}
    

    Object 是对象的名称,model 和 type 是该对象的属性

    【讨论】:

      【解决方案2】:

      我为此使用 ajax 调用

      $('.btnSave').on('click', function(){
          $.ajax({
              url: '@(Url.Action("SavePerson", "Meeting"))',
              type: 'post',
              data: {
                  Value1: 'Value1',
                  Value2: 'Value2'
              },
              success: function (result) {
                  alert('Save Successful');
              }
          });
      });
      

      如果您愿意,可以通过单击按钮或链接单击来调用 href = # 希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        您不能通过查询字符串传递实例复杂类型。这是使用它的方式:

        @Html.ActionLink("Click", "SavePerson", "Meeting", new {x = item.x, y = item.y, ..., type = "participant"}, null)
        

        【讨论】:

        • 但这有效:@Html.ActionLink("Click", "SavePerson", "Meeting", item, null)。使用此格式项正确,但我无法传递第二个参数。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-18
        • 1970-01-01
        • 1970-01-01
        • 2015-05-05
        • 1970-01-01
        • 2015-07-06
        • 1970-01-01
        相关资源
        最近更新 更多