主从创建视图用剃刀,ASP.NET MVC 3和.NET 4.0主从、剃刀、视图、ASP

2023-09-03 09:48:50 作者:若°

我是新来的.NET一起,请耐心等待我,如果我有任何愚蠢的错误。

I'm new to .NET all together, please be patient with me if I have any silly mistakes.

我使用ASP.NET MVC 3与.NET 4.0

I'm using ASP.NET MVC 3 with .NET 4.0

我想有一个创建的视图模型,有一个儿童模特。这种观点应该包括子模型的的部分的 创建的看法,我将使用下面这个简单的例子说明目的:

I want to have a "Create" view for a model that has a child Model. This view should include the child model's partial "Create" view, I'll use the following simple example for illustration purposes:

在人模型

class Person
{
    public string Name { get; set; }
    public Address { get; set; }
}

在地址模型

class Address
{
    public string City { get; set; }
    public string Zip { get; set; }

    //A List for creating a <select/> item in the view
    //containing cities fetched from the database.
    //The initialization is done in the controller action returning
    //the related partial view.
    public IEnumerable<SelectListItem> CityDropDown { get; set; } )
}

控制器操作

The Controller Actions

    class MyController : Controller
    {
        public ViewResult Create()
        {
            var person = new Person();
            var address = new Address();
            // initialization of address.CityDropDown omitted
            person.Address = address;
            return View(MyViews.CreatePersonView, person);
        }

        [HttpPost]
        public ViewResult Create(Person person)
        {
            //persistance logic
        }
    }

我要的意见层次有:

The views hierarchy I want to have :

这是我曾在为了实现这一目标有以下几方面的解决方案:

The solutions that I have tried in order to achieve this are the following :

在个人查看

@model Person
@using(Html.BeginForm()){
    @Html.EditorFor(m=>m.Name)
    @Html.Partial(MyViews.AddressPartialView, @Model.Address)
}

在地址局部视图

ASP NET MVC 2

The Address partial view

@model Address
@Html.EditorFor(m=>m.Zip)
@Html.DropDownListFor(m=>m.City, @Model.CityDropDown)

在提交表单, person.Address 为空。经过一番搜索在谷歌,我发现,为了使地址领域工作的提交,生成的HTML标记必须是下列(注意地址_ preFIX):

When submitting the form, person.Address is null. After a bit of searching on Google, I found out that in order for the submit of the address field to work, the generated HTML markup must be the following (notice the Address_ prefix) :

<form...>
    <input type=text id="Name" />
    <input type=text id="Address_Zip" />
    <select id="Address_City">
        <!-- options... -->
    </select>
</form>

不用说,在我的情况下生成的HTML标记是不一样的,而是它的下面(的地址_ preFIX缺失):

Needless to say, the generated HTML markup in my case isn't the same but instead it's the following (the Address_ prefix is missing) :

<form...>
    <input type=text id="Name" />
    <input type=text id="Zip" />
    <select id="City">
        <!-- options... -->
    </select>
</form>

第二条本办法:使用的 EditorTemplate 的为地址机型

我所做的:

我感动的地址局部视图文件夹的的查看/共享/ EditorTemplates 的确保它具有相同的名称地址模型属性,即 Address.cshtml 的。

Second approach : Using an EditorTemplate for the Address model

What I did :

I moved the Address partial view to the folder View/Shared/EditorTemplates assuring that it has the same name as the Address property in the Person model, i.e Address.cshtml.

在个人查看

@model Person
@using(Html.BeginForm()){
    @Html.EditorFor(m=>m.Name)
    @Html.EditorFor(@Model.Address) //will automatically find the Address 
                             //partial view in the EditorTemplates folder
}

利用这种方法生成的标记实际上已经正确的preFIX(即地址_ ),但我得到一个的的对象未设置以一个实例异常的作为 Address.CityDropDown 属性,它告诉我,在控制器的动作ISN的pre-初始化地址对象吨传递到由于某种原因局部视图。

Using this approach the generated markup has in fact the proper prefix (i.e. Address_), but I get an Object reference not set to an instance exception for the Address.CityDropDown property which tells me that the pre-initialised Address object in the controller's action isn't passed to the partial view for some reason.

此方法适用于没有问题,但我不希望使用它,因为我不希望有多余的code如果我想有一个创建视图的另一种模式的地址。

This approach works with no problems, but I don't want to use it as I don't want to have redundant code if I ever want to have a create view for address in another model.

我应该怎么做才能有一个可重复使用的部分创建视图,我可以用防空火炮我的应用程序?

What should I do in order to have a reusable partial create view that I can use accross my application?

推荐答案

您曾与EditorTemplates正确的做法,但要记住,你需要填充 CityDropDown 。因此,认为应转交给这样的:

You had the correct approach with EditorTemplates, but keep in mind you need to populate the CityDropDown. So, the view should be handed off something like:

Person model = new Person()
{
    Address = new Address
    {
        CityDropDown = new SelectListItem[]{
            new SelectListItem { Selected = true, Text = "Select one..." },
            new SelectListItem { Text = "Anywhere", Value = "Anywhere" },
            new SelectListItem { Text = "Somewhere", Value = "Somewhere" },
            new SelectListItem { Text = "Nowhere", Value = "Nowhere" }
        }
    }
};

这将然后做出这种观点只包含:

Which would then make this view only consist of:

@Html.EditorForModel()

和那么你的 EditorTemplates 便拿起从那里:

And then your EditorTemplates would pick up from there:

〜/查看/共享/ EditorTemplates / Address.cshtml (注:这是基于的键入的没有属性名)

~/Views/shared/EditorTemplates/Address.cshtml (Note: this is based on type not property name)

@model MvcApplication.Models.Address
@Html.DropDownListFor(x => x.City, Model.CityDropDown)
@Html.EditorFor(x => x.Zip)

〜/查看/共享/ EditorTemplates / Person.cshtml

@model MvcApplication.Models.Person
@using (Html.BeginForm())
{ 
    @Html.EditorFor(x => x.Name)
    @Html.EditorFor(x => x.Address)
    <input type="submit" value="Save" />
}

三视图然后渲染是这样的:

the three views then render something like:

<form action="/" method="post">
  <input class="text-box single-line" id="Name" name="Name" type="text" value="" />
  <select id="Address_City" name="Address.City">
    <option selected="selected">Select one...</option>
    <option value="Anywhere">Anywhere</option>
    <option value="Somewhere">Somewhere</option>
    <option value="Nowhere">Nowhere</option>
  </select>
  <input class="text-box single-line" id="Address_Zip" name="Address.Zip" type="text" value="" />
  <input type="submit" value="Save" />

实例项目都可以在这里找到:的https://github.com/bchristie/StackOverflow-Examples/tree/master/questions-19247958

Example project can be found here: https://github.com/bchristie/StackOverflow-Examples/tree/master/questions-19247958