2017-06-07 11 views
0

Usin ASP.NETモデルのオブジェクトにプロファイルのリストを追加し、このリストをビューに列挙しようとしています。しかし、上記のコード ASP.NETのモデルからオブジェクトへのオブジェクトの受け渡し

@foreach (ProfilePage profile in Model.BlogPages) 
      { 
      @Html.Partial("../ProfilePage/Index", new PageViewModel<ProfilePage>(profile)) 
      } 

がエラーを返した

:ビューにこのコードを使用して

 public ActionResult Index(BlogPage currentPage) 
    { 
     var model = new BlogPageModel(currentPage); 

     var pages = new List<BlogPage>(); 

     var profilePages = new List<ProfilePage>(); 
     if (currentPage.ProfileArea != null) 
     { 
      foreach (LinkItem linkItem in currentPage.ProfileArea) 
      { 
       var page = _pageDataHelper.GetPageData(linkItem); 
       var profilePage = page as ProfilePage; 
       if (profilePage != null) 
       { 
        profilePages.Add(profilePage); 
       } 
      } 
      model.Profiles = profilePages; 
     } 
     return View(model); 
    } 

CS0030: Cannot convert type 'Models.Pages.BlogPage' to 'Models.Pages.ProfilePage'

誰かが私に内部のリストを格納するための正しい方法を指すことができますこのネストされたオブジェクトをビューでモデル化してレンダリングしますか?

ありがとうございます!

+0

ProfilePage/IndexのモデルはProfilePageですか?それを確認しましたか? –

+3

'Model.Profiles'を' foreach'したいと思っています –

+1

あなたは 'Model.BlogPages'をループしています。これは上記のスニペットの中のそのプロパティへの唯一の参照です – Jonesopolis

答えて

1

こんにちは、あなたはそれぞれのループで問題があるようですが、モデルが上で利用できないので、私は問題の行を正確に把握できませんでした。あなたの質問へ

回答:

誰かが私にモデル内のリストを格納し、ビューでこのネストされたオブジェクトをレンダリングするための正しい道を指し示すことができますか?

例:

public class somemodelname 
{ 
    public list<anytype> somepropertyname{get;set;} 

} 

アクセス:あなたはモデルの内側に、あなたがいることを上記で行ったのと同じ方法として、ページをレンダリングするための任意のリストオブジェクトを格納することができます上記のようにして

@foreach (var singlevalueOrObj in Model.somepropertyname) 
      { 
      @Html.Partial("../ProfilePage/Index", new PageViewModel<singlevalueOrObj >(profile)) 
      } 

部分図を使用しています。

上記の情報は役に立ちましたか?

カルティク

+0

こんにちは。 – dwigt

+0

私の喜びとそれが少し役に立ちました幸せ.. :) –

1

おかげであなたはforeachループにタイプミスがあります

@foreach (ProfilePage profile in Model.BlogPages) 
{ 
    @Html.Partial("../ProfilePage/Index", new PageViewModel<ProfilePage>(profile)) 
} 

あなたが財産BlogPagesないあなたにProfilePageコレクションに設定されたプロパティProfilesをループしていますコントローラ:

var pages = new List<BlogPage>(); 

var profilePages = new List<ProfilePage>(); 
if (currentPage.ProfileArea != null) 
{ 
    ...shortened for length... 
    model.Profiles = profilePages; // Right here is what you intended to loop over 
} 
関連する問題