简体   繁体   English

ASP.NET MVC在一个视图中创建具有相关对象的对象

[英]ASP.NET MVC Creating an object with related object in one view

I'm new to ASP.NET MVC and I want to create a view where I can create a new object along with the related objects. 我是ASP.NET MVC的新手,我想创建一个视图,在其中可以创建新对象以及相关对象。

As example: I have the following class Person: 例如:我有以下类Person:

public class Person
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual List<Address> Addresses { get; set; }
}

And the class Address: 和班地址:

public class Address
{
    public int AddressId { get; set; }
    public string City { get; set; }
    public string Street { get; set; }

    public int PersonId { get; set; }
    public virtual Person Person { get; set; }
}

What I want to achieve is having a view where the user can type in the data for the person and any number of addresses for this user. 我要实现的是拥有一个视图,用户可以在其中输入此人的数据以及该用户的任意数量的地址。

My first (simple) attempt was simply providing an action link which is mapped to a controller method. 我的第一次(简单)尝试只是提供了一个映射到控制器方法的操作链接。 This method takes a Person object as a parameter, adds a Address object to the collection Addresses and returns a new create view (maybe not nice, but this worked). 此方法将Person对象作为参数,将Address对象添加到集合Addresses中,并返回一个新的create视图(可能不太好,但是可行)。 But when I tried to add a second address I noticed that the Person's collection of addresses was empty again. 但是,当我尝试添加第二个地址时,我注意到此人的地址集合再次为空。

Any sugesstions/best practices for this kind of problem? 是否有针对此类问题的建议/最佳实践?

Thanks! 谢谢!

I would suggest you the following approach: 我建议您采用以下方法:

1.Create a view models: 1.创建视图模型:

public class AddressViewModel
{ 
    public string City { get; set; }
    public string Street { get; set; }
}

public class PersonViewModel()
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public AddressViewModel AddressViewModelTemplate {get; set;}
    public Collection<AddressViewModel> Addresses {get; set;}
}

Then in your view you can use hidden EditorTempalete for the AddressViewModelTemplate , and show it with javascript on some button click. 然后,在您的视图中,可以对AddressViewModelTemplate使用隐藏的EditorTempalete,并在单击某些按钮时使用javascript显示它。 Of course, you will have to adjust the names of the generated collection for the binding. 当然,您将不得不为绑定调整生成的集合的名称。

Sample Editor Template. 样本编辑器模板。 You can choose better structure. 您可以选择更好的结构。

    @model AddressViewModel
    <div>
        <div>@Html.LabelFor(x => x.City)</div>
        <div>@Html.TextBoxFor(x => x.City)</div>
    </div>
    <div>
        <div>@Html.LabelFor(x => x.Street)</div>
        <div>@Html.TextBoxFor(x => x.Street)</div>
    </div>

The in your strongly typed View: 在您的强类型视图中:

@model PersonViewModel;
@using(Html.BeginForm())
{
    //Display textbox for Person properties here
    ....
    <div id="addressInfo" style:"dislpay:none">
        @Html.EditorFor(x => x.AddressViewModelTemplate)
    </div>
    ....
    <div id="AddressesWraper">
        <input id="btnAddAddress" type="button" value="Add new Address" />
    <div>
}
<script>
    $(document).ready(function(){
        $("#btnAddAddress").click(function(){
            $("#AddressesWraper").append("#addressInfo").html();
            refreshAddressNames();
        });
    });
    function refreshAddressNames(){
        /*Here you should name your addresses like this:
          name="AddressViewModel.Addresses[0].City"
          name="AddressViewModel.Addresses[0].Street"
          name="AddressViewModel.Addresses[1].City"
          name="AddressViewModel.Addresses[1].Street"
          ................................[2].City"
                                          [2].Street"

        If you want delete option, you have to mind that too and reorder the collection propely*/
    }
</script>

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

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