从检索视图数据,我应该使用模型绑定? [英] Retrieving data from view, should I use model binder?

查看:98
本文介绍了从检索视图数据,我应该使用模型绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点丢在这里,因为我还没有真正望着模型粘合剂,所以如果可能的话,可以告诉我,如果我其实想我的问题,正确... :)如果我的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.

这篇关于从检索视图数据,我应该使用模型绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆