一个视图的多个模型不起作用

时间:2015-03-27 15:54:30

标签: asp.net-mvc models

我有一个客户模型和我在部分视图中制作的项目模型。

我基本上有一个类,我用于View的两个模型都在主类中。

DataAnnotations对两种模型都运行良好。

当我点击提交按钮并回发时,我正在使用的ViewModel null 用于CustomerDTO& ProjectDTO模型。换句话说,两个类的变量customerProject都为null。

我认为它必须与模型绑定有关。

如果有人能指出我做错了什么,我会非常感激。

请注意,如果我只使用一个类而不将子类包装在超类中,则会填充“customer”对象。

public ActionResult CreateCust(CustomerDTO customer)

这一类的定义如下:

using Models.Models;

namespace Models.ViewModels.Customers
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public class CustomerDTO
    {
        [Key]
        [ScaffoldColumn(true)]
        [Display(Name = "ID")]
        public short CustomerID { get; set; }

        [Required]
        [StringLength(50)]
        public string UserName { get; set; }

        [Required]
        [StringLength(50)]
        [EmailAddress]
        public string Email { get; set; }

        [StringLength(50)]
        public string Company { get; set; }

        [StringLength(50)]
        public string FirstName { get; set; }

        [StringLength(50)]
        public string LastName { get; set; }

        [StringLength(50)]
        public string Address1 { get; set; }

        [StringLength(50)]
        public string Address2 { get; set; }

        [StringLength(50)]
        public string City { get; set; }

        [StringLength(2)]
        public string State { get; set; }

        [StringLength(10)]
        [DataType(DataType.PostalCode)]
        [RegularExpression(@"^\d{5}(-\d{4})?$")]
        public string Zip { get; set; }

        [StringLength(12)]
        [DataType(DataType.PhoneNumber)]
        [RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$")]
        public string HomePhone { get; set; }

        [StringLength(12)]
        [DataType(DataType.PhoneNumber)]
        [RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$")]
        public string CellPhone { get; set; }

        [StringLength(100)]
        [DataType(DataType.Url)]
        public string Website { get; set; }

        [StringLength(50)]
        [DataType(DataType.Url)]
        public string IMAddress { get; set; }

        [DataType(DataType.DateTime)]
        [Display(Name = "Created")]
        public DateTime CreatedDate { get; set; }

        [DataType(DataType.DateTime)]
        [Display(Name = "Updated")]
        public DateTime? UpdatedDate { get; set; }

        public virtual ICollection<Project> Projects { get; set; }
    }
}

这是我的模型(请使用您最喜欢的查看器查看图像以放大它)。 enter image description here

我的主视图定义如下:

@model Models.ViewModels.Customers.CustomerDTO

@{
    ViewBag.Title = "Create Customer & Project";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Create</h2>

@using (Html.BeginForm("CreateCustProj", "Home", FormMethod.Post, new { @id = "frm1", @class = "form-horizontal" }))
{
    <div class="body-content">
        <h4>Customer/Project</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <fieldset>
            <legend>Customer</legend>
            <div class="form-group">
                <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
                    @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control", @placeholder = "UserName" } })
                    @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
                    @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", @placeholder = "Email" } })
                    @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
                    @Html.EditorFor(model => model.Company, new { htmlAttributes = new { @class = "form-control", @placeholder = "Company" } })
                    @Html.ValidationMessageFor(model => model.Company, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-5 col-md-5 col-sm-5 col-xs-5">
                    @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control", @placeholder = "First Name" } })
                    @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                </div>
                <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
                    @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Last Name" } })
                    @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
                    @Html.EditorFor(model => model.Address1, new { htmlAttributes = new { @class = "form-control", @placeholder = "Address1" } })
                    @Html.ValidationMessageFor(model => model.Address1, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
                    @Html.EditorFor(model => model.Address2, new { htmlAttributes = new { @class = "form-control", @placeholder = "Address2" } })
                    @Html.ValidationMessageFor(model => model.Address2, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-6 col-md-6 col-sm-6 col-xs-6">
                    @Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control", @placeholder = "City" } })
                    @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
                </div>
                <div class="col-md-3 col-sm-3 col-xs-3">
                    @Html.EditorFor(model => model.State, new { htmlAttributes = new { @class = "form-control", @placeholder = "State" } })
                    @Html.ValidationMessageFor(model => model.State, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-3 col-md-3 col-sm-3 col-xs-3">
                    @Html.EditorFor(model => model.Zip, new { htmlAttributes = new { @class = "form-control", @placeholder = "Zip" } })
                    @Html.ValidationMessageFor(model => model.Zip, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-5 col-md-5 col-sm-5 col-xs-5">
                    @Html.EditorFor(model => model.HomePhone, new { htmlAttributes = new { @class = "form-control", @placeholder = "Home Phone" } })
                    @Html.ValidationMessageFor(model => model.HomePhone, "", new { @class = "text-danger" })
                </div>
                <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
                    @Html.EditorFor(model => model.CellPhone, new { htmlAttributes = new { @class = "form-control", @placeholder = "Cell Phone" } })
                    @Html.ValidationMessageFor(model => model.CellPhone, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-5 col-md-5 col-sm-5 col-xs-5">
                    @Html.EditorFor(model => model.Website, new { htmlAttributes = new { @class = "form-control", @placeholder = "Website" } })
                    @Html.ValidationMessageFor(model => model.Website, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-5 col-md-5 col-sm-5 col-xs-5">
                    @Html.EditorFor(model => model.IMAddress, new { htmlAttributes = new { @class = "form-control", @placeholder = "IM Address" } })
                    @Html.ValidationMessageFor(model => model.IMAddress, "", new { @class = "text-danger" })
                </div>
            </div>
        </fieldset>

        <fieldset>
            <legend>Project</legend>
            @Html.Partial("_Projects", Model)
        </fieldset>

        <div class="form-group">
            <div>
                <button type="submit" id="btnCustomerProjectCreate" class="btn btn-primary col-offset-2"><span class="glyphicon glyphicon-save"></span>Create</button>
            </div>
        </div>
    </div>
}

我的部分视图(上面注明为@ Html.Partial)如下:

@model Models.ViewModels.Projects.ProjectDTO

<div class="form-group">
    <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
        @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
        @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
        @Html.DropDownListFor(model => model.CategoryID, new SelectList(ViewBag.Categories, "CategoryID", "Description", ViewBag.CategoryID == null ? null : ViewBag.CategoryID), "-- Select Category --", new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
        @Html.DropDownListFor(model => model.PriorityID, new SelectList(ViewBag.Priorities, "PriorityID", "Description", ViewBag.PriorityID == null ? null : ViewBag.Priority), "-- Select Priority --", new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.PriorityID, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
        @Html.DropDownListFor(model => model.StatusID, new SelectList(ViewBag.Statuses, "StatusID", "Description", ViewBag.StatusID == null ? null : ViewBag.StatusID), "-- Select Status --", new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.StatusID, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
        @Html.EditorFor(model => model.Quote, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Quote, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    <div class="col-lg-offset-1 col-md-offset-1 col-sm-offset-1 col-xs-offset-1 col-lg-11 col-md-11 col-sm-11 col-xs-11">
        @Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
    </div>
</div>

2 个答案:

答案 0 :(得分:0)

我认为你会把自己想要传递的内容与如何呈现的内容混淆。 你有两个模型

  1. CustomerDTO
  2. ProjectDTO
  3. 您正在创建ViewModel并将其传递给CustomerDTO类型的View。当您将相同模型传递给partial @ Html.Partial(“_ Projects”,Model)时,您正在传递CustomerDTO。但是,部分观点预计会收到ProjectDTO。意味着你应该通过ProjectsDTO。

    现在我可以看到你有   public virtual ICollection Projects {get;组; } 在那里设置,我假设通过查看你的命名空间声明中的Projects包含ProjectDTO模型。

    使用

    通过foreach循环传递ProjectDTO模型
    @foreach(var item in Model.Projects)
    {
       @Html.Partial(item.ProjectDTO)
    }
    

    这会奏效。但是,这不是最佳做法。您应该获得ICollection并使用DisplayTemplate来渲染数据。

    请发布您的ProjectsDTO模型,并发布您想要的预期输出,以便我们可以具体回答出错的地方,

    <强>更新 我想我现在明白事情的发展方向错了。在我解释的时候,我会尽量保持简单。

    你说过,你的模型回归为空。这可能有三个原因。

    1. 您从未实例化过您的模型。通过(CustomerDTO cDTO = new CustomerDTO()
    2. 来做
    3. 从某个地方获取它,例如CustomerDTO cDTO = getCustomerDTO(),其中getCustomerDTO()函数重新调整CustomerDTO模型。
    4. 即使您有一个模型,但您从未将其传递给View。您可以执行以下操作中的操作。

      Public ActionResult ShowCustomer() { CustomerDTO cDTO = new CustomerDTO(); return View(“ViewName”,cDTO); }

      转到第二个问题,你想要一个包含多个类的超类。

    5. 你有DTO是你的DomainModels,其工作是从数据库获取数据,我认为他们正在做他们的工作。否如果这两个域模型都需要转到View,您需要创建看起来像这样的ViewModel,

          public class DTOViewModel
      {
       public CustomerDTO Customer {get ; set ;}
       public ProjectsDTO Projects {get; set;}
      }
      

      然后你可以创建看起来像下面的行动,

      public ActionResult DTOViews()
      {
       DTOViewModel model = new DTOViewModel();
      
      model.Customer = getCustomer(); 
      model.Projects = getProjects();
      
      return View("YourView" , model); //Make sure ViewName exist in Respective Folder.
      }
      

      然后在视图中您可能有以下内容;

       @model namespace.DTOViewModel
      
          <h2> This is your main View. </h2>
      
          @Html.Partial("_CustomerPartial" , model.Customer )
          @Html.Partial("_ProjectPartial" , model.Projects )
      

      希望这有帮助。

答案 1 :(得分:0)

检查生成的帮助程序的Html。您需要表单元素的名称类似于&#34; CustomerProjectDto.CustomerDto.Description&#34;。使用Partials,我得到了&#34;描述&#34;。

尝试使用您自己的编辑器模板,为复杂的节点创建正确的html。

查看此文章以了解相关信息。 https://lostechies.com/jimmybogard/2011/09/07/building-forms-for-deep-view-model-graphs-in-asp-net-mvc/