我将模型切回了一个领域:
//模型
public class LetterViewModel { public string LetterText; }
//控制器
public ActionResult Index() { var model = new LetterViewModel(); model.LetterText = "Anything"; return View(model); } [HttpPost] public ActionResult Index(LetterViewModel model) { //model.LetterText == null return View(model); }
//视图
@model Test.Models.LetterViewModel @{ Layout = "~/Views/Shared/_Layout.cshtml"; ViewBag.Title = "Create a Letter"; } @using (Html.BeginForm()) { <div id="Bottom"> @Html.TextAreaFor(m => m.LetterText) <input type="submit" value="Ok" class="btn btn-default" /> </div> }
当我在开发工具中检查“网络”选项卡时,它显示输入的值已包含在请求中。但是,当HttpPost控制器被激发时,该字段为空。
在DefaultModelBinder不设置字段,仅属性的值。您需要更改模型以包含属性
DefaultModelBinder
public class LetterViewModel { public string LetterText { get; set; } // add getter/setter }