简体   繁体   English

如果单个部分需要多个对象,如何从视图中动态添加项目到我的集合?

[英]How to dynamically add items to my collection from a view when single partial requires multiple objects?

I have a list of items displayed in my view. 我有一个在我的视图中显示的项目列表。 I need to be able to manually add items to this list from the UI with an add button. 我需要能够通过添加按钮从UI手动添加项目到此列表。 I know I need to load a partial view with javascript to get the new item to be displayed. 我知道我需要使用javascript加载局部视图以显示新项目。

@{var count = 0;}
@foreach (var dx in Model.Patient.DxList)
{    
    <div class="form-field">
        <label>@(++count)</label>
        @Html.HiddenFor(d => dx.DxIndex)
        @Html.TextBoxFor(d => dx.Dx, new { @class = "small" })
        <span class="description">@((dx.DxRef != null) ? dx.DxRef.Title : "")</span>
        @Html.DropDownListFor(d=> dx.Indicator, new SelectList(Model.Codes, "Value", "Description", dx.Indicator), "Please select", new { @class = "" })
    </div>
}

However, because my item contains both a TextBox and a DropDownList, which is populated with Codes, does my partial view need a ViewModel too? 但是,因为我的项目包含TextBox和DropDownList(使用代码填充),我的部分视图是否也需要ViewModel? To contain not only the Dx, but the list of appropriate Codes to create the list? 不仅要包含Dx,还要包含创建列表的相应代码列表?

public class DxSingleViewModel
{
    public Dx Dx { get; set; }
    public List<Code> Codes { get; set; }
}

No idea if this is the general approach, or if I'm off base. 不知道这是否是一般方法,或者我是否偏离基础。

You should separate the rendering of one item to a separate partial view / editor template and call it inside your foreach loop, so you have jsut one place where you define the markup for one item: 你应该将一个项目的渲染分离到一个单独的局部视图/编辑器模板,并在foreach循环中调用它,这样你就可以在一个地方定义一个项目的标记:

@foreach (var dx in Model.Patient.DxList)
{
    @Html.EditorFor(m => dx)
}

Then your AddNewItem action can return a partial view which just calls @Html.EditorForModel() 然后你的AddNewItem动作可以返回一个只调用@Html.EditorForModel()的局部视图

When adding and removing rows dynamically, you should embed your own index (for example a guid string) to the items to avoid collisions. 在动态添加和删除行时,您应该将自己的索引(例如guid字符串)嵌入到项目中以避免冲突。 Take a look at this great article which explains how to implement this with a custom html helper: http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ 看一下这篇很棒的文章,它解释了如何使用自定义的html帮助程序来实现它: http//blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2 -样式/

Your EditorTemplate will look like this: 您的EditorTemplate将如下所示:

@using (Html.BeginCollectionItem("DxList"))
{
    @Html.TextBoxFor(d => dx.Dx, new { @class = "small" })
    ....
}

Regarding your question about whether the viewmodel should contain the list for your dropdown or not: The viewmodel should contain every data you need to render the view. 关于viewmodel是否应该包含下拉列表的问题:viewmodel应该包含呈现视图所需的每个数据。 So i would say yes, its a good place to pass the list to the view. 所以我会说是的,它是将列表传递给视图的好地方。

Of cause you could pass it in the viewbag too, but i'm a friend of strong typed data bags, so i would prefer the viewmodel. 因为你也可以在viewbag中传递它,但我是强类型数据包的朋友,所以我更喜欢viewmodel。

You can try this approach. 你可以尝试这种方法。 It has one downside that you have to remember name (path) of the property to render the right input name for automatic binding (so you cannot easily change it with refactoring tool). 它有一个缺点,你必须记住属性的名称(路径),以呈现自动绑定的正确输入名称(因此您无法使用重构工具轻松更改它)。

View 视图

@foreach (int i=0;i < Model.Patient.DxList.Count;i++)
{
    @{Html.RenderPartial("PartialDx", Model.Patient.DxList[i] ,new ViewDataDictionary() { {"count",i}});}
}

PartialView - strongly typed partial view with type of DxSingleViewModel PartialView - 类型为DxSingleViewModel强类型局部视图

@{string collectionName = string.Format("Patient.DxList[{0}].", ViewBag.count);}

<div class="form-field">
   <label>@{ViewBag.count + 1}</label>
   @Html.Hidden(collectionName+"Dx.DxIndex", Model.Dx.DxIndex)

</div>

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

相关问题 MVC从单个视图创建多个对象 - MVC creating multiple objects from single view ASP.NET MVC 如何从嵌套对象的视图模型集合中动态创建表结构 - ASP.NET MVC How to create table structure dynamically in view from viewmodel collection of nested objects 如何将项目从组合框添加到列表集合 - How To Add Items From a ComboBox to a List Collection 为什么我的DataGrid带有ObservableCollection <T> 当我从集合中添加或删除项目时,显然不会刷新? - Why is my DataGrid with a ObservableCollection<T> apparently not refreshing when I add or remove items from the collection? 如何将多个项目添加到ItemsSource WPF多个集合绑定中? - how to add multiple items into ItemsSource WPF multiple collection binding? 如何在ASP.NET MVC视图中动态添加和删除订单项? - How do I dynamically add and remove line items from an ASP.NET MVC view? Silverlight DataServices将项目动态添加到集合中 - Silverlight DataServices add items dynamically to the collection 如何在单个视图中添加多个模型? - How to add multiple model in a single view? 如何使用jQuery从asp.net视图模型动态删除行,而不删除集合中其余的下一个对象 - How to delete rows dynamically with jquery from an asp.net view model without deleting the rest of the next objects in the collection 多个任务处理单个工作项集合 - Multiple tasks processing a single collection of work items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM