asp.net-mvc-2 – asp.net-mvc2 – 不使用Model的强类型助手?
发布时间:2020-12-15 22:33:02 所属栏目:asp.Net 来源:网络整理
导读:在MVC2中使用强类型帮助程序时,在发布帖子时不会从Model属性中获取输入字段值.这是默认行为吗? 强类型助手的(强类型)视图: div class="editor-label" %: Html.LabelFor(model = model.Name) %/divdiv class="editor-field" %: Html.TextBoxFor(model = mod
在MVC2中使用强类型帮助程序时,在发布帖子时不会从Model属性中获取输入字段值.这是默认行为吗?
强类型助手的(强类型)视图: <div class="editor-label"> <%: Html.LabelFor(model => model.Name) %> </div> <div class="editor-field"> <%: Html.TextBoxFor(model => model.Name) %> <%: Html.ValidationMessageFor(model => model.Name) %> </div> <div class="editor-label"> <%: Html.LabelFor(model => model.Price) %> </div> <div class="editor-field"> <%: Html.TextBoxFor(model => model.Price) %> <%: Html.ValidationMessageFor(model => model.Price) %> </div> 控制器操作:/ Product / Edit / 5 public ActionResult Edit(int id) { var p = new Product(); p.Name = "product 1"; p.Price = "100"; return View(p); } Html输出: <div class="editor-label"> <label for="Name">Name</label> </div> <div class="editor-field"> <input id="Name" name="Name" type="text" value="product 1" /> </div> <div class="editor-label"> <label for="Price">Price</label> </div> <div class="editor-field"> <input id="Price" name="Price" type="text" value="100" /> </div> 控制器操作:/ Product / Edit / 5 [HttpPost] public ActionResult Edit(Product p) { p.Name = "prrrrrrd 2"; return View(p); } 表单发布后的Html输出(下面我希望输入id =“Name”的值为“prrrrrrd 2.强类型帮助器从哪里得到它的值?): <div class="editor-label"> <label for="Name">Name</label> </div> <div class="editor-field"> <input id="Name" name="Name" type="text" value="product 1" /> </div> <div class="editor-label"> <label for="Price">Price</label> </div> <div class="editor-field"> <input id="Price" name="Price" type="text" value="100" /> </div> 解决方法
是的,它们首先从ModelState中获取,然后从模型中获取.如果您打算在POST操作中对模型执行某些修改,则需要先从ModelState中删除它们.例如: [HttpPost] public ActionResult Edit(Product p) { ModelState.Remove("Name"); p.Name = "prrrrrrd 2"; return View(p); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET MVC 6和ASP.NET Core 1.0之间有什么区别以及核心框
- asp.net-mvc – Unity使用参数将依赖项注入MVC??过滤器类
- asp.net – 站点之间共享的IIS 7.0应用程序池 – 站点之间是
- asp.net CustomValidator从不触发OnServerValidate
- asp.net-mvc – ASP.NET MVC 6中的基本视图页面
- asp.net-core – 我为什么要选择带有.Net核心的ASP.NET Cor
- asp.net-mvc-3 – jQuery.validator.unobtrusive.adapters.
- iis-6 – IIS 6上的ASP.NET路由
- asp.net – 以编程方式编辑Web.config
- asp.net – 如何为可自定义的应用程序设置数据模型
推荐文章
站长推荐
- asp.net – 如何创建自定义数据注释验证器
- 在HiddenField asp.net中存储一个字符串数组
- asp.net – 什么时候Response.IsClientConnected
- asp.net-mvc – 为什么在HttpGet操作上的mvc属性
- asp.net-mvc – 如何最好地实现保存|保存并关闭|
- asp.net – 有谁知道如何摆脱我的.net网络服务的
- 使用ASP.NET构建了多少个公共高流量网站?
- asp.net-mvc – MVC(ASP.NET MVC)乐队3层架构如何
- ASP .NET MVC 3 – 如何提交嵌套在html表单中的a
- asp-classic – 使用经典ASP区分测试和生产的最佳
热点阅读