从检索视图数据,我应该使用模型绑定?

编程入门 行业动态 更新时间:2024-10-17 07:25:08
本文介绍了从检索视图数据,我应该使用模型绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有点丢在这里,因为我还没有真正望着模型粘合剂,所以如果可能的话,可以告诉我,如果我其实想我的问题,正确... :)如果我的code是方式,请指教...

1 - 我有一个包含一个DTO类的自定义字段各自的名称和其他属性即:

公共类CustomFields{ 公共字符串名称{;设置;} 公共字符串描述{获取;设置;} 公共字符串的FieldType {获取;设置;} 公共字符串值{获得;设置;}}

2 - 在我的回购/业务层我设定值和视图返回的ICollection呈现

3的视图使用一个foreach显示领域

<%的foreach(在型号VAR项){%GT;   < D​​IV CLASS =编辑标记>      &所述;%= Html.Label(item.Name)%GT;   < / DIV>   < D​​IV CLASS =主编场>      &所述;%= Html.TextBox(item.Name,item.Value)%GT;   < / DIV><%}%GT;

问题:什么是检索通过后的结果的最佳方式?如果有错误,我需要将错误发送回视图...

请注意:我做了什么 - >

[HttpPost][ValidateAntiForgeryToken]公众的ActionResult指数([ModelBinder的(typeof运算(CustomModelBinder))的ICollection< CustomFields>字段){// code这里...}

自定义模型绑定从形式收集得到的价值观和在转换到正确的类型 - 这是正确的?最好的办法做到这一点?我感觉我过于复杂的事情...

公共类CustomModelBinder:IModelBinder{ 公共对象BindModel(ControllerContext controllerContext, ModelBindingContext的BindingContext) {  VAR领域=新的List< CustomFields>();  VAR的FormCollection =新的FormCollection(controllerContext.HttpContext.Request.Form);  的foreach(在字符串的FormCollection _key)  {    如果(_key.Contains(_ RequestVerificationToken))         打破;    fields.Add(新CustomFields {名称= _key,    值= formCollection.GetValue(_key).AttemptedValue});  }  返回领域; }}

解决方案

一切都是完美的,直到第3步,你提到foreach循环在视图中。这就是我会停下来使用的编辑器模板来代替。因此,通过更换循环在您的视图:

<%= Html.EditorForModel()%GT;

和将被渲染为模型集合中的每个元素对应的编辑器模板内(〜/查看/共享/ EditorTemplates / CustomFields.ascx ):

< D​​IV CLASS =编辑标记>   &所述;%= Html.LabelFor(X => x.Name)%GT;< / DIV>< D​​IV CLASS =主编场>   &所述;%= Html.TextBoxFor(X => x.Name)%GT;< / DIV>< D​​IV CLASS =主编场>   &所述;%= Html.TextBoxFor(X => x.Value)%GT;< / DIV>< D​​IV CLASS =主编场>   &所述;%= Html.TextBoxFor(X => x.Description)%GT;< / DIV>< D​​IV CLASS =主编场>   &所述;%= Html.TextBoxFor(X => x.FieldType)%GT;< / DIV>

后来干脆:

[HttpPost][ValidateAntiForgeryToken]公众的ActionResult指数(IEnumerable的< CustomFields>字段){    // code这里...}

无需任何型号的粘合剂。编辑模板将采取使他们正确绑定生成输入字段专有名词的照顾。

I am a bit lost here as I have not really looked at model binders so if possible, can one advise me if I am actually thinking about my problem correctly... :) and if my code is way of, please advise...

1 -I have a DTO class which contains 'custom fields' each with a name and other properties i.e.:

Public Class CustomFields { public string Name {get;set;} public string Description {get;set;} public string FieldType {get;set;} public string Value {get;set;} }

2- Within my repo/business layer I am setting the values and returning ICollection for the view to render

3- the view uses a foreach to display fields

<% foreach (var item in Model) {%> <div class="editor-label"> <%= Html.Label(item.Name) %> </div> <div class="editor-field"> <%= Html.TextBox(item.Name, item.Value)%> </div> <% } %>

Question: What is the best way to retrieve the result via a post? If there are errors I need to send back the errors to the view...

NOTE: What I did-->

[HttpPost] [ValidateAntiForgeryToken] public ActionResult Index([ModelBinder(typeof(CustomModelBinder))] ICollection<CustomFields> Fields) { //code here... }

Custom Model binder gets values from form collection and transforms in into the correct type- is this correct? The best way to do this? I get the feeling I overcomplicated things...

public class CustomModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var fields = new List<CustomFields>(); var formCollection = new FormCollection(controllerContext.HttpContext.Request.Form); foreach (string _key in formCollection) { if (_key.Contains("_RequestVerificationToken")) break; fields.Add(new CustomFields { Name = _key, Value = formCollection.GetValue(_key).AttemptedValue }); } return fields; } }

解决方案

Everything is perfect until step 3 where you mention foreach loops in a view. That's where I would stop and use editor templates instead. So replace the loop in your view by:

<%= Html.EditorForModel() %>

and inside the corresponding editor template which will be rendered for each element of the model collection (~/Views/Shared/EditorTemplates/CustomFields.ascx):

<div class="editor-label"> <%= Html.LabelFor(x => x.Name) %> </div> <div class="editor-field"> <%= Html.TextBoxFor(x => x.Name) %> </div> <div class="editor-field"> <%= Html.TextBoxFor(x => x.Value) %> </div> <div class="editor-field"> <%= Html.TextBoxFor(x => x.Description) %> </div> <div class="editor-field"> <%= Html.TextBoxFor(x => x.FieldType) %> </div>

then simply:

[HttpPost] [ValidateAntiForgeryToken] public ActionResult Index(IEnumerable<CustomFields> fields) { //code here... }

No need of any model binders. The editor template will take care of generating proper names for the input fields so that they are correctly bound.

更多推荐

从检索视图数据,我应该使用模型绑定?

本文发布于:2023-11-17 12:29:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1610005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   绑定   模型   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!