如何在模型中将行添加到集合列表?

编程入门 行业动态 更新时间:2024-10-11 07:27:46
本文介绍了如何在模型中将行添加到集合列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为简单起见,假设我有一个User模型,其中List<Email>作为其属性之一.

For the sake of simplicity, let's say I have a User model that has a List<Email> as one of its properties.

public class UserModel { public string UserName { get; set; } public List<Email> Emails = new List<Email>(); } public class Email { public string Address { get; set; } }

在我看来,我有一封电子邮件列表:

In my view, I have a list of the emails:

<table> @foreach(Email email in Model.Emails) { <tr> <td>@Html.EditorFor(modelItem => email.Address)</td> </tr> } </table>

现在让我们说我希望用户能够单击向表添加新行的按钮,以便用户可以向绑定到其用户的列表中添加新的电子邮件.我该怎么做呢?我是否需要以某种方式通过javascript添加新行,以便在发布页面时将其绑定到模型?我不知道该如何解决这个问题,因为我是WebForms的MVC相对较新的人.

Now let's say I want the user to be able to click a button that adds a new row to the table so that the user can add a new Email to the List that is bound to their User. How do I do this? Do I need to add the new row via javascript in a certain way so that it gets bound to the model when the page is posted? I have no idea how to approach this as I'm relatively new to MVC coming from WebForms.

推荐答案

经过研究,我发现了Steven Anderson blog.stevensanderson/2010/01/28/editing-a-variable-length-list-aspnet-mvc- 2种样式/

After some researching, I found this blog post by Steven Anderson blog.stevensanderson/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

它似乎完全可以满足我的要求(除了它是用MVC2编写的).

It appears to be doing exactly what I want (except it is written in MVC2).

更多推荐

如何在模型中将行添加到集合列表?

本文发布于:2023-11-12 11:19:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   模型   如何在   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!