问题描述
当我在下面使用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");
}