简体   繁体   English

如何在父视图中添加或编辑孩子

[英]How to add or edit a child in parent view

I am new in MVC. 我是MVC的新手。 I display the parent view details in two parts, the details and a list of child. 我分两部分显示父视图详细信息,详细信息和子列表。 But now I want add the possibility to add an other child to this parent in the same view. 但是现在我想添加在同一视图中向该父级添加另一个子级的可能性。 I use : controller 我用:控制器

public ViewResult Details(long id)
    {
        Parent x = db.Parent.Find(id);
        return View(x);
    }

view : 查看:

@model  MyProject_v0_2.Models.Parent

<table id="details">
....
</table>

<table id="lstChild">
    @foreach (var item in Model.Child)
            {...}
</table>

I want add a button here which let me add a child in this view. 我想在此处添加一个按钮,让我在此视图中添加一个孩子。 I can add a table that I display or not with all the child data, but I don't know I to save it. 我可以添加一个显示或不显示所有子数据的表,但是我不知道要保存该表。 In the future I will need to change some Parent properties so I can't change the model type. 将来,我将需要更改某些“父级”属性,因此无法更改模型类型。

Thanks, 谢谢,

Checkout the following article which illustrates a nice way of how to achieve this task. 请查看以下文章 ,该文章说明了如何完成此任务的好方法。 It uses javascript on the client to add/delete rows. 它在客户端使用javascript来添加/删除行。 It also uses a custom Html.BeginCollectionItem helper which allows for generating non sequential indexes for the collection input field names. 它还使用自定义的Html.BeginCollectionItem帮助程序,该帮助程序可为集合输入字段名称生成非顺序索引。

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

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