繁体   English   中英

为什么 POST 方法返回空值?

[英]Why POST method return nulls?

当我在下面使用 POST 方法时,我可以在我的控制器中看到空值(当我在调试模式下将鼠标悬停在“文档”上时)。 为什么会发生? 两列都有值。 我尝试将值从这些字段传递给控制器​​,但没有传递到那里。

看法:

@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", "Home", 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");
}

你在帖子中的参数是空的原因是因为它不能 .NET 不能假设你发送的对象是他期望的实际对象,在这种情况下你应该做的是告诉它,它将在请求的主体,为了使其工作,您只需要在名为 FromBody 的签名中添加一个装饰器

[HttpPost]
public ActionResult Update([FromBody] DocumentsModel docs)
{
    //some code

    return RedirectToAction("Index");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM