c# – 控制器后期操作不接收模型
发布时间:2020-12-16 01:52:46 所属栏目:百科 来源:网络整理
导读:非常基本的型号: public class Person{ public string Name; public int Age;} 而且非常简单的观点: @model DynWebPOC.Models.Person@{ Layout = "~/Views/Shared/_Layout.cshtml";}Hello,@Model.Namebr/You're getting old at @Model.Age years old now!@
非常基本的型号:
public class Person { public string Name; public int Age; } 而且非常简单的观点: @model DynWebPOC.Models.Person @{ Layout = "~/Views/Shared/_Layout.cshtml"; } Hello,@Model.Name <br/> You're getting old at @Model.Age years old now! @using(Html.BeginForm("Index","Test",FormMethod.Post)) { <fieldset> <label for="name" style="color: whitesmoke">Name:</label> @Html.TextBoxFor(m => m.Name) <br/> <label for="age" style="color: whitesmoke">Age:</label> @Html.TextBoxFor(m => m.Age) <br/> <input type="submit" value="Submit"/> </fieldset> } 一个非常简单的控制器: public class TestController : Controller { [HttpGet] public ActionResult Index() { object model = new Person {Name = "foo",Age = 44}; return View(model); } [HttpPost] public ActionResult Index(Person person) { return View(); } } 加载屏幕时,值正确绑定到页面.但是当我按下提交按钮时,person对象具有age和amp;的所有空值.名称. 因为我使用了Html.TextBoxFor,它不应该正确设置所有绑定并且对象应该自动绑定回POST吗?它在GET中绑定得很好.. 我在Html.BeginForm()的调用中错过了什么? 解决方法
您必须在模型中创建属性
public class Person { public string Name { get; set; } public int Age { get; set; } } 代替 public class Person { public string Name; public int Age; } ASP.net MVC仅绑定属性. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |