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

发布于 2024-10-29 05:37:03 字数 1592 浏览 3 评论 0原文

我在这里有点迷失,因为我没有真正看过模型活页夹,所以如果可能的话,如果我真的正确地考虑了我的问题,可以告诉我...:)如果我的代码是这样的,请建议...

1 - 我有一个 DTO 类,其中包含“自定义字段”,每个字段都有名称和其他属性,即:

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

2 - 在我的存储库/业务层中,我设置值并返回 ICollection 以供视图渲染

3 - 视图使用 foreach显示字段

<% 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>
<% } %>

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

注意:我做了什么-->

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

自定义模型绑定器从表单集合中获取值并将其转换为正确的类型 - 这是正确的吗?最好的方法是什么?我感觉我把事情搞得太复杂了...

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;
 }
}

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;
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-11-05 05:37:03

一切都很完美,直到第 3 步提到视图中的 foreach 循环。这就是我会停下来并使用编辑器模板的地方。因此,将视图中的循环替换为:

<%= Html.EditorForModel() %>

并在相应的编辑器模板内替换该模板,该编辑器模板将为模型集合的每个元素呈现(~/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>

然后简单地:

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

不需要任何模型活页夹。编辑器模板将负责为输入字段生成正确的名称,以便正确绑定它们。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文