0

2 つのモデルがあり、1 つのビューで表示したい。だから私は使用しています

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

    }

これは私の 2 番目のモデルです。

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

どうすればそれを愛することができますか?

4

2 に答える 2

2

graduandModel の graduandDegreeGroup プロパティのインスタンスを作成していません。したがって、この行:

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

あなたが言ったように例外をスローします。単純に、2 番目のパラメーターが 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

于 2012-10-11T06:00:47.960 に答える
1

別のオプションは、上記の 2 つのモデルを 1 つに結合する新しいビュー モデルを作成することです。そうすれば、このビューに必要なすべてのデータのプロパティが含まれます。次に、部分ビューへの呼び出しでモデルを指定する必要はありません。親のモデルが自動的に使用されます。あるいは、結合モデルを使用することで、ビューをパーシャルに分割する必要がまったくない場合もあります。異なるビューごとに固有のビュー モデルを持つことは珍しくありません。一部のアプリケーションでは、2 つの異なるビューが同じデータを必要とすることはめったにありません。

結合されたビュー モデル:

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 に答える