如何解决为什么POST方法返回null?
当我在下面使用POST方法时,我可以在控制器中看到null(在调试模式下将鼠标悬停在“ docs”上时)。为什么会发生?两列都有值。我尝试将值从这些字段传递给控制器,但没有任何传递给控制器。
查看:
@using (Html.BeginForm("Update","Home",FormMethod.Post))
{
<table>
<tr>
<td>
@Html.HiddenFor(modelItem => item.Account) //it allows me send values for POST/GET methods
@Html.displayFor(modelItem => item.Account)
</td>
<td class="choice">
@Html.TextBoxFor(modelItem => item.PaymentDate,new { @type = "date",@class = "form-control datepicker" })
</td>
<td>
@Html.ActionLink("Save","Update",new { nrAccount = item.Account,manualDate = item.PaymentDate },null)
</td>
</tr>
</table>
}
型号:
public class DocumentsModel
{
public string Account { get; set; }
public DateTime? PaymentDate { get; set; }
}
控制器:
[HttpPost]
public ActionResult Update(DocumentsModel docs)
{
//some code
return RedirectToAction("Index");
}
解决方法
您的帖子中的参数为null的原因是因为它不能.NET无法假定您发送的对象是他期望的实际对象,在这种情况下,您应该告诉它,它将在请求的正文中,为了使其能够正常工作,您只需要向Decorator添加一个名为FromBody的签名
[HttpPost]
public ActionResult Update([FromBody] DocumentsModel docs)
{
//some code
return RedirectToAction("Index");
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。