2012-10-11 81 views
0

我有兩個模型,我想在一個視圖中顯示。所以我正在使用傳入字典的模型項目類型爲'yyyy',但此字典需要'xx'類型的模型項目

@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; } 

    } 

這是畝控制器

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'. 

我該如何解決它?

+1

我是不是失蹤了嗎?我沒有看到graduandModel.graduandDegreeGroup被設置(這意味着它是空的) – ThatSteveGuy

+0

public graduandDegreeModel graduandDegreeGroup {get;組; }這一個是在GraduandModel – user1348351

+1

你的控制器動作CheckData實際上並沒有設置graduandDegreeGroup - 因爲它是null,所以你將null傳遞給@ Html.Partial助手。因此,不是使用(partialViewName,model)的重載,而是使用Partial(partialViewName)並在主模型上發送 –

回答

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> 
} 
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 passed into the dictionary is of type

相關問題