简体   繁体   English

如何绑定列表 <T> 在ASP.Net MVC C#的表单发布中

[英]How to bind List<T> on Form Post in ASP.Net MVC C#

I have a model that contains a list property List<EducationalBackground> EducationalBackground and in the razor form I want the user to enter multiple institutions. 我有一个包含列表属性List<EducationalBackground> EducationalBackground的模型,并且以剃须刀的形式我希望用户输入多个机构。

Model 模型

public class Application
{
    ...
    [Required(ErrorMessage = "Required Field")]
    public List<EducationalBackground> EducationalBackground { get; set; }
    ...
    public Application() 
    {
        ...
        EducationalBackground = new List<Library.EducationalBackground>();
        ...
    }
}

Here is the class: 这是课程:

public class EducationalBackground
{
    public string InstituteName { get; set; }
    ...
}

In the razor view I am trying like this 在剃刀视图中,我正在尝试这样

@Html.TextBoxFor(m => m.EducationalBackground[0].InstituteName, new { @class = "form-control" })

but obviously it doesn't work because the list ( EducationalBackground ) is empty. 但显然不起作用,因为列表( EducationalBackground )为空。

The user will have a "Add New Institution" button, so the initial list size is unknown 用户将具有“添加新机构”按钮,因此初始列表大小未知

How do I properly accomplish this? 我如何正确地做到这一点?

UPDATE Found my solution. 更新找到了我的解决方案。 Look for my post in the answer section 在答案部分中查找我的帖子

I am not quite sure what is the purpose of your view to edit a list of EducationalBackground or to add new ones. 我不太确定您的视图用于编辑EducationalBackground列表或添加新视图的目的是什么。 If you want to edit them you obviously need to take them from somewhere like database for example. 如果要编辑它们,则显然需要从数据库之类的地方获取它们。

  1. Edit case: 编辑大小写:

In the action you have: 在动作中,您有:

EducationalBackground = new List<Library.EducationalBackground>
{
     new EducationalBackground(...),
     new EducationalBackground(...),
     new EducationalBackground(...),
     ..... //How many you want
}

In the view you have: 在视图中,您具有:

@foreach(var educationbackground in Model)
{
    @Html.TextBoxFor(m => educationbackground.InstituteName, new { @class = "form-control" })
}
  1. Add new ones. 添加新的。

If you want to add new ones and as you have answered you may use simple html syntax to achieve that: If you dont know the size of the list you want to add you may choose another way like in jQuery or angularjs to do this for you: 如果您想添加新列表,并且已经回答,则可以使用简单的html语法来实现:如果您不知道要添加列表的大小,则可以选择jQuery或angularjs中的另一种方法:

<script type="text/javascript">
    $(document).ready(function(){
        var size = 0;
        $("#btnAdd").click(function(){
             $(".someDiv").append("<input type='text' name='EducationalBackground["+size+"].InstituteName' class='form-control' />");
             size++;
        })
    })
</script>

EDIT for fiddle 编辑小提琴

Take a look at this fiddle here 看看这个小提琴这里

Change the constructor of the Application class to this: Application类的构造函数更改为此:

EducationalBackground = new List<Library.EducationalBackground>() 
{
    new Library.EducationalBackground()
};

I found my answer. 我找到了答案。 I can bind multiple EducationalBackground items like this 我可以像这样绑定多个EducationalBackground项目

<input type="text" name="EducationalBackground[0].InstituteName" class="form-control" />
<input type="text" name="EducationalBackground[1].InstituteName" class="form-control" />
<input type="text" name="EducationalBackground[2].InstituteName" class="form-control" />

When the form posts, it will create a list of size 3 表单发布后,它将创建一个大小为3的列表

The user will have a "Add New Institution" button, so following this pattern, I will use JS to generate, on the fly, new input elements 用户将有一个“添加新机构”按钮,因此按照此模式,我将使用JS即时生成新的input元素

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

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