简体   繁体   English

ASP.Net MVC ViewModel包含列表 <anotherViewModel> 动态为anotherViewModel添加列表条目

[英]ASP.Net MVC ViewModel contains List<anotherViewModel> dynamically add list entry for anotherViewModel

i cant find and solution how to dynamically add a ViewModel entry through another ViewModel thats loaded in the Edit View. 我找不到并解决如何通过加载在编辑视图中的另一个ViewModel来动态添加ViewModel条目。

Lets say i have a ViewModel thats contains a List of anotherViewModel. 可以说我有一个ViewModel,它包含另一个ViewModel的列表。

Database Model: 数据库模型:

public class ViewModel
{
    public ViewModel()
    {
        anotherViewModel= new List<anotherViewModel>();
    }

public string idViewModel{ get; set; }
public string description{ get; set; }
public List<anotherViewModel> aViewModel { get; set; }
}

The User loads the Edit View containing the ViewModel, that currently has 2 entrys of type anotherViewModel. 用户加载包含ViewModel的编辑视图,该视图当前具有2个类型为anotherViewModel的条目。

the user should have the possibility to add another entry of type anotherViewModel to the ViewModel and even change the properties from the anotherViewModel entrys. 用户应该可以将另一个类型anotherViewModel的条目添加到ViewModel中,甚至可以更改anotherViewModel条目的属性。

the another ViewModel is in an partial view. 另一个ViewModel在局部视图中。 i tryd to reload it with ajax, if i do so, the changes made in the current model, are lost. 我试图用ajax重新加载它,如果这样做,当前模型中所做的更改将丢失。 because i cant hand over the model the controller. 因为我无法交出控制器模型。

i know there must be some solution with jquery, but i cant find one. 我知道jquery必须有一些解决方案,但是我找不到。

thanks for your help. 谢谢你的帮助。

The traditional way (in pseudo code): in your view you'd create something like: 传统方式(用伪代码):在您的视图中,您将创建以下内容:

<form>
Your html code for ViewModel properties.
create list of partials:
@foreach(anotherModel in Model.aViewModel)
{
   @Html.Partial("_yourPartialView", anotherViewModel)
}
Code for adding a new anotherView element.
ie: @Html.Partial("_createNewElement", new anotherViewModel)
<submit button>
</form>

Your page will list the anotherViewModel list, and in the partial view you have the html markup for anotherViewModel. 您的页面将列出anotherViewModel列表,在部分视图中,您将具有anotherViewModel的html标记。 When the user has edited the existing ones and/or added a new element, the submit button will post the whole viewmodel with the list of anotherViewModels to your action. 当用户编辑了现有视图和/或添加了新元素后,“提交”按钮将把整个viewmodel和anotherViewModels列表发布到您的操作中。 That action will then handle the adds and updates. 然后,该操作将处理添加和更新。

However, this does tend to only work for applications where the chance of concurrency exceptions are low. 但是,这确实仅适用于并发异常几率低的应用程序。 A smoother way of doing it is to have each partial view take responsibility for the data it contains and persist it through ajax. 一种更平滑的方法是让每个局部视图对其包含的数据负责,并通过ajax对其进行持久化。 This will negate the need for posting all the changes in one go. 这样就无需一次性发布所有更改。

Your viewmodel will be translated with the help of razor helpers, into html elements. 您的视图模型将在剃须刀助手的帮助下转换为html元素。 When the users edits the viewmodels data on the client you actually edit html. 当用户在客户端上编辑viewmodels数据时,您实际上是在编辑html。 When you post back to the controller the default model binder in mvc will try to convert your post data into the model the controller action is expecting as a parameter. 当您发布回控制器时,mvc中的默认模型绑定程序将尝试将发布数据转换为控制器动作期望作为参数的模型。 When you have complex models it gets a bit tricky. 当您拥有复杂的模型时,它会变得有些棘手。 Have you tried to build a custom model binder? 您是否尝试过构建自定义模型活页夹?

public class CustomBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, 
                        ModelBindingContext bindingContext)
{
    HttpRequestBase request = controllerContext.HttpContext.Request;

    string idViewModel= request.Form.Get("idViewModel");
    string description= request.Form.Get("description");
    var list = new List<anotherViewModel>(); //create list from form data


    return new ViewModel
               {
                   idViewModel= idViewModel,
                   description= description,
                   aViewModel = list
               };
}

} }

and in your controller action: 并在您的控制器操作中:

public ActionResult Edit([ModelBinder(typeof(CustomBinder))] ViewModel vm)

Otherwise, Maybe you should rethink your data structure. 否则,也许您应该重新考虑您的数据结构。 Maybe have a separate partial view where you edit the anotherViewModel. 也许有一个单独的局部视图,您可以在其中编辑anotherViewModel。

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

相关问题 如何使用 ASP.NET Core 3.1 MVC 中的 ViewModel 与 JavaScript 和 C# 动态添加到列表 - How to dynamically add to list with ViewModel in ASP.NET Core 3.1 MVC with JavaScript and C# 在ViewModel中包含List属性的Post Back List之后的ASP.NET MVC为空 - ASP.NET MVC After Post Back List that contains List property in ViewModel is null Asp.net MVC 4.5使用EditorFor,Button Click在ViewModel中将对象添加到列表 - Asp.net MVC 4.5 Add Object to List in ViewModel with EditorFor, Button Click ASP.NET MVC - map 视图中的布尔值到视图模型中的整数列表 - ASP.NET MVC - map bools in View to List of ints in ViewModel Asp.Net MVC Razor下拉菜单-从ViewModel访问列表 - Asp.Net MVC Razor Dropdown - Accessing list from ViewModel ASP.NET Core 6.0 MVC - 从列表中填充视图模型 - ASP.NET Core 6.0 MVC - Populating a viewmodel from list ASP.NET MVC - 为元素列表更新 ViewModel - ASP.NET MVC - Update ViewModel for List of elements ASP.NET 核心MVC列表<model> .包含(模型)</model> - ASP.NET Core MVC List<Model>.Contains(Model) 如何将集合列表添加到Asp.net MVC中的列表 - How to Add Collection List to a List in Asp.net MVC 包含 asp.net 核心 3 MVC 中的列表属性的 model 的表单 - form for a model that contains a list property in asp.net core 3 MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM