简体   繁体   English

为具有List对象属性的模型生成创建视图

[英]Generate create view for model which have List object property

I'm new to MVC. 我是MVC的新手。 I have a model class which is having List property of another class. 我有一个模型类,它具有另一个类的List属性。

public class CustomerModel
{
    [Required]
    [Display(Name = "Customer Name")]
    public string CustomerName { get; set; }

    [Required]
    public string Company { get; set; }

    [Required]
    [Display(Name="Contact Details")]
    public List<ContactModel> Contacts { get; set; }
}

public class ContactModel
{
    [Required]
    [Display(Name = "Contact Name")]
    public string ContactName { get; set; }

    [Required]
    [Display(Name = "Contact Number")]
    public string ContactNo { get; set; }
}

When I create the view for create action, Visual studio just create markup only for ContactName and ContactNo. 当我创建用于创建操作的视图时,Visual Studio只为ContactName和ContactNo创建标记。

Current UI is like this. 当前的UI就是这样。

没有用于插入联系人的UI。

But I need a UI like this. 但是我需要这样的UI。

在此处输入图片说明

Is there a way to generate markup for Contacts property insertion. 有没有一种方法可以生成用于联系人属性插入的标记。 Or should I need to do this kind of thing with jquery and custom json calls. 或者我应该使用jquery和自定义json调用来做这种事情。

There is a way to do this kind of UI. 有一种方法可以执行这种UI。 I'll show you how to do it. 我会告诉你如何做。 To do it, we have to use partial views and ajax calls. 为此,我们必须使用局部视图和ajax调用。

First you have to do some changes on CustomerModel. 首先,您必须对CustomerModel进行一些更改。

    public class CustomerModel
    {
        [Required]
        [Display(Name = "Customer Name")]
        public string CustomerName { get; set; }

        [Required]
        public string Company { get; set; }

        [Required]
        public ContactModel ContactModel {get;set;}

        [Required]
        [Display(Name="Contact Details")]
        public List<ContactModel> Contacts { get; set; }
    }   

Now you have to add a partial view which generate your contact list. 现在,您必须添加一个局部视图以生成您的联系人列表。 Here I added a partial view called _Contacts.cshtml 在这里,我添加了一个名为_Contacts.cshtml的局部视图

@model CustomerModel


@for (int i = 0; i < Model.Contacts.Count; i++)
{
    <tr>
        @Model.Contacts.Count
        <td class="editor-field">
            @Html.EditorFor(model => model.Contacts[i].ContactName)
        </td>


        <td class="editor-field">
            @Html.EditorFor(model => model.Contacts[i].ContactNo)
            @Html.ValidationMessageFor(model => model.Contacts[i].ContactNo)
        </td>

    </tr>
}

Now you have to add a another ActionResult method which returns a partial view. 现在,您必须添加另一个ActionResult方法,该方法返回局部视图。

    [HttpPost]
    public ActionResult GenerateContacts(CustomerModel customer)
    {
        // Check whether this request is comming with javascript, if so, we know that we are going to add contact details.
        if (Request.IsAjaxRequest())
        {
            ContactModel contact = new ContactModel();
            contact.ContactName = customer.ContactMode.ContactName;
            contact.ContactNo = customer.ContactMode.ContactNo;

            if (customer.Contacts == null)
            {
                customer.Contacts = new List<ContactModel>();
            }

            customer.Contacts.Add(contact);

            return PartialView("_Contact", customer);
        }            
    }

Now we write a onclick event for "Add Contact" button. 现在,我们为“添加联系人”按钮编写一个onclick事件。 In there we pass ViewModel data to the above action method and retrieve the generated contact view. 在这里,我们将ViewModel数据传递给上述action方法,并检索生成的联系人视图。

I assume that "Add Contact" button's Id is addContact. 我假设“添加联系人”按钮的ID为addContact。 Here I add generated html or the contact details to a table. 在这里,我将生成的html或联系方式添加到表中。

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

            $.ajax({
                type: "POST",
                url: 'Customer/GenerateContacts', // get the link to the controller's action method
                data: form.serialize()
            })
            .success(function (html) {
                // this function will automatically called when response of the called ajax call is success
                var tableBody = $("#tblContactBody");
                tableBody.text(html);
            })
            .error(function (msg) {
                console.log(msg);
            });

        });
    });

Hope you get this. 希望你能得到这个。

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

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