2011-11-03 116 views
0

讓我解釋一下我的問題:傳遞多個模型,視圖和partialViews在Asp.net MVC 3

我必須爲使用實體框架對象創建的四個表。 我已經添加一個存儲庫類到實體模型來添加/刪除/獲取/查詢我需要的東西。

public class YPlaylistRepository 
{ 

    private aspnetdbEntities entities = new aspnetdbEntities(); 
    // 
    // Query Methods 
    public IQueryable<Song> FindAllSongs() 
    { 
     return entities.Songs; 
    } 

    public IQueryable<TopTenFav> FindAllTopTen() 
    { 
     return entities.TopTenFavs; 
    } 

    public IQueryable<Genre> FindAllGenres() 
    { 
     return entities.Genres; 
    } 
} 

等等...

我的索引視圖可分爲幾個局部視圖,如:

@{ 
ViewBag.Title = "Home Page"; 
    } 

@Html.Partial("_PartialPlayer") 
<div> 

    @Html.Partial("_PartialOtherFav") 
<div id="topTenContainer" style="float: left; width:450px;margin-top:49px;"> 
@Html.Partial("_PartialTopTenFav") 
@Html.Partial("_PartialCurrentFav") 

讓的說,在我的_PartialOtherView我有我想要的一種形式鍵入一些信息並將其添加到數據庫中:

@model yplaylist.Models.TopTenFav 

<div id="otherFavContainer"> 

<div id="txtYoutubeLinkContainer"> 
@using (Html.BeginForm("AddTopTenFav", "Home", FormMethod.Post, new { id = "AddTopTenFavForm" })) 
{ 

    <span id="youTubeLinkSpan">Youtube Link</span> 
    <div> 
     @Html.TextBoxFor(modelItem => modelItem.YoutubeLink, new { id ="youTubeLinkTxt" }) 
    </div> 
    <span id="youTubeNameSpan">Song Title</span> 
    <div> 
     @Html.TextBoxFor(modelItem => modelItem.Title,new{id="youTubeNameTxt"}) 
    </div> 

<button type="submit" name="btnCreateComment" value="">submit</button> 
} 

</div> 
</div> 

</div> 

該請求發送到控制器:

public class HomeController : Controller 
{ 
    private YPlaylistRepository repository = new YPlaylistRepository(); 


    public ActionResult Index() 
    { 
     var topTenList = repository.FindAllTopTen().ToList(); 
     return View(topTenList); 
    } 

    public ActionResult About() 
    { 
     return View(); 
    } 

    public ActionResult Users() 
    { 

     return View(); 
    } 

    [HttpPost] 
    public ActionResult AddTopTenFav(TopTenFav topTen) 
    { 

     topTen.Date = DateTime.Now; 
     topTen.UserName = User.Identity.Name; 
     repository.AddTopTen(topTen); 
     repository.Save(); 
     return RedirectToAction("Index"); 
    } 

} 

我將如何解決通過正確的模型,以我的索引視圖的問題,當我所有的partialviews將是處理不同型號..我想盡量創造一個封裝我的所有車型的一類,但這只是因爲我的實體對象返回了在我的「HomeViewModel」中找不到的特定類型(如對象列表等)而產生了更多的問題,因此我真的很困惑,我會如何解決這個問題,我確信它可以完成不知何故,但最新的方式是什麼?在此先感謝

+0

「我的實體對象返回了在我的」HomeViewModel「中找不到的特定類型,例如對象列表等等」這是什麼意思? – gdoron

+0

public ActionResult Index() var topTenList = repository.FindAllTopTen()。ToList(); return View(topTenList); } –

+0

我返回了一個對象列表,但這只是我的一個部分視圖,其他部分視圖將需要其他數據,我將如何解決這個問題?我不能直接發送結果到我的偏好?只有我的主要觀點我猜 –

回答

2

我認爲(從我對問題的理解),你需要的是通過包含任何進一步的車型,如視圖模型到索引視圖:

public class IndexModel 
{ 
    public TopTenFav TopTenFavourites { get; set; } 

    ... 
} 

然後在指數( )動作,你將返回視圖模型:

public ActionResult Index() 
{ 
    var topTenList = repository.FindAllTopTen().ToList(); 
    return View(new IndexModel() { TopTenFavourites = topTenList}); 
} 

那麼該視圖通過這個模型/從作爲局部視圖:

@Html.Partial("_PartialTopTenFav", Model.TopTenFavourites) 

在部分視圖中提交表單應該調用AddTopTenFav()並正確地將TopTenFav模型傳遞給該操作。

+0

在理論上這正是我想要的但是當我嘗試這樣做,你知道整個故事:D謝謝我會給這個鏡頭 –

+0

TopTenFavourites = topTenList –

+0

我不能明確地將topTenList轉換爲對象,因爲它是一個對象列表,任何想法如何解決它? –

相關問題