2011-11-03 181 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转换为对象,因为它是一个对象列表,任何想法如何解决它? –

相关问题