English 中文(简体)
• 如何收集MVC3中弹 post的图像?
原标题:How to get subcollections on viewmodel on postback in MVC3?

14. Phil Haack有一个条款,说明如何确定货物,使违约模式人对背后收集物品具有约束力:

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

我面临的问题是,我不仅试图向主计长行动发回一笔款项,而且要收集一份汇编。

我的班子基本如此:

public class MyViewModel
{
public int IncidentNumber { get; set; }
public string StoreId { get; set; }
public string RepId { get; set; }
public string OrderStatus { get; set; }
public CustomerViewModel Customer { get; set;
//... other properties and SelectLists for binding

public IEnumerable<OrderItemViewModel> OrderItemViewModels { get; set; }

事实上,我可以把客户电文Model数据重新贴在后台上,但命令书目清单是空的。 我怎样回来? Phil s article isn t helping it.

最佳回答

我在此有类似的问题:https://stackoverflow.com/questions/6585585/mvc- binding-to-model-with-list-property-ignores-other-properties”。 MVC对以清单形式列出财产具有约束力,忽视了其他财产,而后者则在意见中采用以下代码加以解决。

<div class="editor-field">
@for (int i = 0; i < Model.MyCollection.Count(); i++)
{
    @Html.HiddenFor(m => m.MyCollection[i].Id)
    @Html.HiddenFor(m => m.MyCollection[i].ParentId)
    @Html.HiddenFor(m => m.MyCollection[i].Name)
    @Html.TextBoxFor(m => m.MyCollection[i].Value)
}
</div>
问题回答

Use an editor template:

@model MyViewModel
@using (Html.BeginForm())
{
    ... some input fields 

    @Html.EditorFor(x => x.OrderItemViewModels)

    <input type="submit" value="OK" />
}

然后在相应的编辑模板内,自动为<代码>的每个要素提供。 OrderItemViewModels Collection (~/Views/joint/EditorTemplates/OrderItemViewModels.cshtml):

@model OrderItemViewModels
<div>
    @Html.LabelFor(x => x.Prop1)
    @Html.EditorForFor(x => x.Prop1)
</div>
<div>
    @Html.LabelFor(x => x.Prop2)
    @Html.EditorForFor(x => x.Prop2)
</div>
...




相关问题
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. ...

热门标签