English 中文(简体)
伙伴关系中可计数的示范约束力IE。 NET MVC POST?
原标题:Modelbinding IEnumerable in ASP.NET MVC POST?

是否有任何问题对一个MVC POST具有示范约束力的电子计算类型?

我的《示范公约》中的一些财产没有受到某一员额的约束,因此无法采取行动。 Seems that property on the model such as strings are ok, but my IE amountable is what is not being subject.

这里是我法典的一个要点:

<%: Html.TextBoxFor(m => m.ResponseInfo.SubsetInfo.Test) %>
    <% for (int i = 0; i < Model.ResponseInfo.SubsetInfo.BandAvailabilities.Count(); i++)
    {%>
        <%: Html.TextBoxFor(m => m.ResponseInfo.SubsetInfo.BandAvailabilities.ToArray()[i].BandName) %>
  <% } %>

这里是这些特性在模式中所看的:

public IEnumerable<BandAvailabilityInfo> BandAvailabilities { get; set; }
public string Test { get; set; }

该观点对其中预期价值的文本箱清单进行了细化和产出。 但是,所发射的邮政行动只承认试验扼杀是一种财产。 模型状态也不包含我的可计算数据。

最佳回答

具有约束力的模式取决于生成的html。 . . . .

<input type="text" name = "ResponseInfo.SubsetInfo.BandAvailabilities[0].BandName"/>
<input type="text" name = "ResponseInfo.SubsetInfo.BandAvailabilities[1].BandName"/>
<input type="text" name = "ResponseInfo.SubsetInfo.BandAvailabilities[2].BandName"/>
.
.
<input type="text" name = "ResponseInfo.SubsetInfo.BandAvailabilities[n].BandName"/>

i have not tried it but i am almost certain that call to ToArray method in loop is keeping the system from generating proper names for nested inputs. There are couple of things you can do to remedy this First, in your view model change

public IEnumerable<BandAvailabilityInfo> BandAvailabilities { get; set; }

to

public IList<BandAvailabilityInfo> BandAvailabilities { get; set; }  //or Array

so you don t have to call ToArray method in the loop and proper names are generated for inputs. Second, make an editor template and put it in Editor templates folder either under the current controller or in shared folder s Editor template folder. Make this view accept model of type BandAvailabilityInfo and name of this view should also be BandAvailabilityInfo. then in your main view you only have to replace entire loop with

 <%: Html.EditorFor(m => m.ResponseInfo.SubsetInfo.BandAvailabilities%>

其余部分将由框架本身处理。

问题回答

It works fine with the IEnumarble type.
I think the problem is the ToArray you are writing inside the For loop. (It s very not efficient either)
change the for loop to foreach.

。 出色地回答如何实现这一目标。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签