【问题标题】:Passing an ID from Razor view to Controller将 ID 从 Razor 视图传递到控制器
【发布时间】:2015-11-23 17:45:43
【问题描述】:

在 List 类型的视图中,我试图将视图中的项目 id(在本例中为电子邮件)传递给控制器​​中的 actionResult:我使用了以下代码():

 @Html.ActionLink("Contact", "CreateMessage", "Member", new { id = item.eMail }) 

我得到一个空引用,因为传递的 id 是空的。

这是工作正常的视图,其中的 id 不为空:

@model IEnumerable<Mutuelle.Domain.Entities.Adherent>

@{
    ViewBag.Title = "lister";
}

<h2>Liste des adhérents</h2>


<table class="table">
    <tr>
        <body style="overflow:scroll;">
            <th>
                @Html.DisplayNameFor(model => model.dateOfBirth)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.address)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.phoneNumber)
            </th>
            <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>


            <td>
                @Html.DisplayFor(modelItem => item.dateOfBirth)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.address)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.phoneNumber)
            </td>
            <td>
                @Html.ActionLink("Contacter", "CreateMessage", "MembreComite", new { id = item.eMail })

            </td>
        </tr>
    }

</table>

这是控制器:

public ActionResult CreateMessage(string id)
        {
            Message message = new Message();
            return View(message);

        }
    [HttpPost]
        public ActionResult CreateMessage(Message message, string id)
        {

            if (ModelState.IsValid)
            {
                AMDMService service = new AMDMService();

                Member member = service.GetMemberByeMail(id);
                message.member = member;

                message.adherentId = id;



                member.listMessages.Add(message);

                return RedirectToAction("listeMessages");

            }
            return View(message);

        }

【问题讨论】:

  • 哪里有错误?
  • 这是导致空引用错误的行(无法将值 NULL 插入到adherentId列中):'message.adherentId = id;'因为从视图传递的 id 是 NULL
  • 这是表单 POST 提交吗?表格的其余部分在哪里?用浏览器的网络监视器观察请求——请求体是什么样的?它与您的动作签名匹配吗?此外,foreach 在发送集合时很少一起工作。请改用for 循环。见here
  • 这是整个表格。 “for”不起作用,它会导致错误,视图是使用类型自动生成的“List”被强类型化到实体“member”
  • 如果您尝试执行 GET 操作,则您使用的覆盖错误:ActionLink(linkText, action, controller, htmlAttributes) 而不是 ActionLink(linkText, action, controller, routeValues, htmlAttributes)

标签: razor asp.net-mvc-5 nullreferenceexception


【解决方案1】:

当您在 ActionLink 中使用像 new { id = item.eMail } 这样的 routeValues 时,您总是应该为 html attributes 提供值,它主要用于设置链接的样式,所以如果您不想设置样式你的链接然后你传递null,否则你传递类似new { @class = "btn btn-rounded btn-blue" }的东西)。因此,如果您不想设置链接样式,ActionLink 的代码应如下所示:

 @Html.ActionLink("Contact", "CreateMessage", "Member", new { id = item.eMail }, null);

您也可以将@Html.ActionLink 助手替换为@Url.Action 助手,如下所示:

<a href="@Url.Action("CreateMessage", "Member", new { id = item.eMail })"> Contact</a>

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多