简体   繁体   English

如何在剃刀视图中动态地将数据添加到视图模型的List属性中?

[英]How can I dynamically add data to a List property in view model in a razor view?

I have a client needs to be able to create candidates. 我有一个客户需要能够创建候选人。 Candidates can have many qualifications (qualifications is model with 4 properties). 候选人可以具有许多资格(资格是具有4个属性的模型)。 The client needs to be able to add N numbers of qualifications to the employee on the creation page. 客户需要能够在创建页面上向员工添加N个资格。

View Model 查看模型

public class CreateCandidateViewModel
{
    [DisplayName("First Name"), Required]
    public string FirstName { get; set; }

    [DisplayName("Last Name"), Required]
    public string LastName { get; set; }

    [DisplayName("Email Address"), Required]
    public string Email { get; set; }

    [DisplayName("Phone Number"), Required]
    public string Phone { get; set; }

    [DisplayName("Zip Code"), Required]
    public int ZipCode { get; set; }

    public List<Qualification> Qualifications { get; set; }
}

Qualification Model 资格模型

public class Qualification
{
    [Key]
    public int Id { get; set; }
    public int QualificationTypeId { get; set; }
    public string Name { get; set; }
    public DateTime DateStarted { get; set; }
    public DateTime DateCompleted { get; set; }

    [ForeignKey("QualificationTypeId")]
    public virtual QualificationType Type { get; set; }
}

I have no idea how to approach this problem. 我不知道如何解决这个问题。 I was thinking of creating the candidate first and then sending the client to another view where the client can add qualifications and so on. 我当时正在考虑先创建候选人,然后再将客户发送到另一个可以让客户添加资格的视图。

Stephen mentioned in a comment that you will likely need to use javascript or jquery to accomplish this if you want to keep your user on the same page. Stephen在评论中提到,如果您想让用户保持在同一页面上,则可能需要使用javascript或jquery来完成此操作。

I am assuming your post controller is expecting a CreateCandidateViewModel 我假设您的后控制器期望使用CreateCandidateViewModel

It is possible to do model binding when your model has a property of a list of objects, so long as the naming on the inputs is correct when the form is submitted. 只要您的模型具有对象列表的属性,就可以进行模型绑定,只要在提交表单时输入的命名正确即可。 The key is indexing the names on the inputs: 关键是索引输入中的名称:

<input type="text" name="Qualifications[0].Id" />
<input type="text" name="Qualifications[0].QualificationTypeId" />
<input type="text" name="Qualifications[0].Name" />
<input type="text" name="Qualifications[0].DateStarted" />
<input type="text" name="Qualifications[0].DateCompleted" />
<input type="text" name="Qualifications[1].Id" />
<input type="text" name="Qualifications[1].QualificationTypeId" />
<input type="text" name="Qualifications[1].Name" />
<input type="text" name="Qualifications[1].DateStarted" />
<input type="text" name="Qualifications[1].DateCompleted" />

This will correctly bind to your model on submit. 这将在提交时正确绑定到您的模型。 Be mindful of resetting the indexies when removing a "qualification" after it was added, or you may get null objects in your list, or missing objects. 添加“限定词”后删除它们时,请务必重置索引,否则列表中可能会出现空对象或缺少对象。 I've done this successfully before with JQuery and regular expressions. 在使用JQuery和正则表达式之前,我已经成功完成了此操作。

If you want to go the Ajax way, you could create a partial view and call that using AJAX. 如果要使用Ajax方式,则可以创建局部视图并使用AJAX进行调用。

Controller 调节器

public ActionResult QualificationsPartial(Int32 Index)
{
    return PartialView(model:Index);
}

Partial View 部分视图

@model Int32

<input type="text" name="Qualifications[@Model.ToString()].Id" />
<input type="text" name="Qualifications[@Model.ToString()].QualificationTypeId" />
<input type="text" name="Qualifications[@Model.ToString()].Name" />
<input type="text" name="Qualifications[@Model.ToString()].DateStarted" />
<input type="text" name="Qualifications[@Model.ToString()].DateCompleted" />

Ajax on Main View Ajax主视图

var QualificationIndex = parseInt(1);  // 1 because we already have 0 loaded

$("#AddQualificationElement").click(function () {

    $.ajax({
        cache: false,
        type: "GET",
        url: "/MyController/QualificationsPartial",
        data: { 
           Index: QualificationIndex
        },
        success: function (data) {
            // data will be the html from the partial view
            $("#QualificationsContainer").append(data);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            // Handle the error somehow
        }
    }); // end ajax call
}); // end AddQualificationElement click event

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

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