简体   繁体   English

使用EditorFor(model [])时传回单个对象

[英]Passing back a single object when using EditorFor(model[])

This has been a thorn in my side for a while. 一段时间以来,这一直困扰着我。 If I use EditorFor on an array of objects and the editor Template has a form in it ex. 如果我在对象数组上使用EditorFor并且编辑器模板在其中有一个窗体。

public class FooController:Controller {

    public ActionResult Action(Foo foo) {
        // ...
    }
}

Index.cshtml Index.cshtml

@model IEnumerable<Foo>
@Html.EditorFor(m=> m)

EditorTemplate 编辑器模板

@model Foo
@using (Html.BeginForm("action", "controller"))
{
    @Html.TextBoxFor(f=> f.A)
    @Html.CheckBoxFor(f=> f.B)
    @Html.LabelFor(f=> f.B)
}

So I'll hit a few problems. 所以我会遇到一些问题。

The checkbox label's for doesn't bind correctly to the checkbox (This has to do with the label not receiving the proper name of the property ( [0].A as opposed to A ). 复选框标签的for不能正确绑定到复选框(这与标签未接收到属性的正确名称( [0].A而不是A )。

I'm aware I can get rid of the pre- text by doing a foreach on the model in Index but that screws up ids and naming as the framework doesnt realize there are multiples of the same item and give them the same names. 我知道我可以通过在Index中的模型上进行foreach来摆脱前言,但是由于框架没有意识到同一项目存在多个并赋予它们相同的名称,因此搞砸了id和命名。

For the checkboxes I've just been doing it manually as such. 对于复选框,我只是这样手动进行。

@Html.CheckBoxFor(m => m.A, new {id= Html.NameFor(m => m.A)})
<label for="@Html.NameFor(m => m.A)">A</label>

However I cant solve the inability of the controller to accept the item as a single model. 但是,我无法解决控制器无法将项目作为单个模型接受的问题。 I've even tried allowing an array of Foo's in the Action parameters but that only work when its the first item being edited ( [0]... ) if its any other item in the array (ex. [1].A ) the controller doesn't know how to parse it. 我什至尝试过在Action参数中允许Foo的数组,但仅当它的第一个项目( [0]... )被编辑(如果数组中的其他任何项目(例如[1].A )时才起作用)控制器不知道如何解析它。 Any help would be appreciated. 任何帮助,将不胜感激。

Make your model a class with the properties you need. 使模型成为具有所需属性的类。 create a class in your Models subfolder 在您的模型子文件夹中创建一个类

public class MyModel { 
public IEnumerable<Foo> Foolist { get ; set;} 
public string Something { get;set;}
}

your EditorFor will have to have a foreach loop for Foolist... MVC will attempt to put your model together from the form and return it to your POST action in the controller. 您的EditorFor必须为Foolist提供一个foreach循环... MVC将尝试将您的模型从表单中放在一起,并将其返回到控制器中的POST操作。

Edit: 编辑:

You could create an EditorTemplate for foo. 您可以为foo创建一个EditorTemplate。 In Views/Shared/EditorTemplates folder, create FooTemplate.cs 在“视图/共享/ EditorTemplates”文件夹中,创建FooTemplate.cs

@model Foo
<div class="span6 float-left" style="margin-bottom: 6px">
    @Html.TextBoxFor(m => m.A, new { style = "width:190px" })
    @Html.CheckBoxFor(m => m.B, new { style = "width:40px" })
    @Html.ValidationMessage("foo", null, new { @class = "help-inline" })
</div>

then in your view 然后在您看来

@foreach (var myFoo in Model)
    {
      @EditorFor(myFoo)
    }

This still suffers from the "model gets passed back as a whole" requiredment of yours. 这仍然遭受您的“模型整体传回”的要求。 Not sure about why there is a need to process these individually. 不确定为什么需要单独处理这些内容。

Hah finally solved this - Here's how I did it. 哈哈终于解决了这个-这是我的方法。 As a bit of background HTML forms use the name attribute when submitting forms, but the label for element uses Id . 作为背景,HTML表单在提交表单时使用name属性,但是element的标签使用Id。 so I only adapt the id tag to have the prefix and not the name tag. 因此,我只将id标签改编为具有前缀而不是name标签。

--In the cshtml file -在cshtml文件中

@{
  var prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
  ViewData.TemplateInfo.HtmlFieldPrefix = "";
}

then I can specify the id for the properties by their prefix while letting the name remain the same like so 那么我可以通过属性的前缀指定属性的ID,同时让名称保持不变

  @Html.CheckBoxFor(m => m.A, 
                    new {id = prefix+"."+ Html.NameFor(m => m.A)}) 
 <label for="@prefix.@Html.NameFor(m => m.A)">A!</label></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM