0

我有两个模型,我想在一个视图中显示。所以我正在使用

 @Html.Partial

这是我的第一个模型。

public partial class graduandModel :BaseNopEntityModel 
    {

        public graduandModel()
        {
            this.AvailableCeremony = new List<SelectListItem>();

        }


        public string first_name { get; set; }

        public string middle_name { get; set; }

        public string last_name { get; set; }

        public int student_id { get; set; }

        public int ceremony_id { get; set; }

        public DateTime ceremony_date { get; set; }

        public int graduand_id { get; set; }

        public IList<SelectListItem> AvailableCeremony { get; set; }

        public graduandDegreeModel graduandDegreeGroup { get; set; }

    }

这是我的第二个模型。

 public class graduandDegreeModel
    {

        public graduandDegreeModel()
        {
            this.AvailableDegree = new List<SelectListItem>();

        }

        public  string degree_id { get; set; }
        public  int graduand_id { get; set; }

        public  string degree_name { get; set; }

        public IList<SelectListItem> AvailableDegree { get; set; }

    }

这是 mu 控制器

public ActionResult CheckData(int ceremony_id, string first_name, string middle_name, string last_name)
        {
          graduandModel model = new graduandModel();

             graduandDegreeModel model_1 = new graduandDegreeModel();
             var graduandList = _graduandService.GetGraduandByStudent(ceremony_id, first_name, middle_name, last_name);             

               if (graduandList.Count != 0)
               {
                   model.ceremony_id = ceremony_id;
                   model.first_name = first_name;
                   model.middle_name = middle_name;
                   model.last_name = last_name;

                  // var degreeList = "";

                   foreach (var c in graduandList)
                   {

                       var degreeList = _graduandDegreeService.getAllDegreeIdBtGraduandId(c.graduand_id);

                       foreach (var d in degreeList)
                       {
                           model_1.AvailableDegree.Add(new SelectListItem() { Text = d.Degree.degree_name, Value = d.degree_id });
                       }
                   }
               }
               return View(model);           

        }

这是我的看法

@{
    Layout = "~/Views/Shared/_ColumnsThree.cshtml";
}



@model graduandModel
@using Nop.Web.Models.Hire;
@using Nop.Web.Framework;
@using Telerik.Web.Mvc.UI;
@using System.Linq;

 @using (Html.BeginForm())
 { 
<table  >

 <tr>
    <td >
      Ceremony : 
    </td>
    <td>
       Ceremony at @Model.ceremony_date

    </td>
</tr>

  <tr>
            <td >
              Name :
            </td>
            <td >
               @Model.first_name  @Model.middle_name  @Model.last_name
            </td>
        </tr>



</table>

     <div>


          @Html.Partial("_DegreeDetailsByGraduand", Model.graduandDegreeGroup)


     </div>
 }

这是我的部分观点

@{
    Layout = "~/Views/Shared/_ColumnsThree.cshtml";
}



@model graduandDegreeModel
@using Nop.Web.Models.Hire;
@using Nop.Web.Framework;
@using Telerik.Web.Mvc.UI;
@using System.Linq;


<table  >

 <tr>
    <td >
      AAAAAA
    </td>
    <td>
        @Html.DropDownListFor(model => model.degree_id, Model.AvailableDegree)
       @* @Html.ValidationMessageFor(model => model.ceremony_id)*@
    </td>
</tr>



</table>

有错误

The model item passed into the dictionary is of type 'Nop.Web.Models.Hire.graduandModel', but this dictionary requires a model item of type 'Nop.Web.Models.Hire.graduandDegreeModel'.

我怎么能爱它???

4

2 回答 2

2

您没有为 graduandModel 的 graduandDegreeGroup 属性创建实例。所以这一行:

@Html.Partial("_DegreeDetailsByGraduand", Model.graduandDegreeGroup)

会像你说的那样抛出异常。仅仅因为第二个参数是NULL。

您可以尝试修改 graduandModel 的构造函数如下:

    public graduandModel()
    {
        this.AvailableCeremony = new List<SelectListItem>();
        this.graduandDegreeGroup = new graduandDegreeModel();

    }

异常应该消失了。您可能还会发现此链接很有帮助:ASP.NET MVC renderpartial, model item passing into the dictionary is of type

于 2012-10-11T06:00:47.960 回答
1

您的另一种选择可能是创建一个新的视图模型,将上述两个模型组合为一个。这样,它就具有此视图所需的所有数据的属性。然后你不需要在调用局部视图时指定模型,它会自动使用父模型。或者,您可能根本不需要使用组合模型将视图分成部分。每个不同的视图都有一个唯一的视图模型并不少见。在某些应用程序中,很少有两个不同的视图需要相同的数据。

组合视图模型:

public class CheckDataViewModel 
    {
        public CheckDataViewModel ()
        {
            this.AvailableCeremony = new List<SelectListItem>();
            this.AvailableDegree = new List<SelectListItem>();
        }
        public string first_name { get; set; }
        public string middle_name { get; set; }
        public string last_name { get; set; }
        public int student_id { get; set; }
        public int ceremony_id { get; set; }
        public DateTime ceremony_date { get; set; }
        public int graduand_id { get; set; }
        public IList<SelectListItem> AvailableCeremony { get; set; }
        public graduandDegreeModel graduandDegreeGroup { get; set; }
        public  string degree_id { get; set; }
        public  string degree_name { get; set; }
        public IList<SelectListItem> AvailableDegree { get; set; }
    }

综合观点:

@{
    Layout = "~/Views/Shared/_ColumnsThree.cshtml";
}



@model CheckDataViewModel
@using Nop.Web.Models.Hire;
@using Nop.Web.Framework;
@using Telerik.Web.Mvc.UI;
@using System.Linq;

 @using (Html.BeginForm())
 { 
<table  >

 <tr>
    <td >
      Ceremony : 
    </td>
    <td>
       Ceremony at @Model.ceremony_date

    </td>
</tr>

  <tr>
            <td >
              Name :
            </td>
            <td >
               @Model.first_name  @Model.middle_name  @Model.last_name
            </td>
        </tr>



</table>

     <div>


         <table  >

 <tr>
    <td >
      AAAAAA
    </td>
    <td>
        @Html.DropDownListFor(model => model.degree_id, Model.AvailableDegree)
       @* @Html.ValidationMessageFor(model => model.ceremony_id)*@
    </td>
</tr>



</table>


     </div>
 }
于 2012-10-11T12:04:45.157 回答