让我解释一下我的问题:传递多个模型,视图和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”中找不到的特定类型(如对象列表等)而产生了更多的问题,因此我真的很困惑,我会如何解决这个问题,我确信它可以完成不知何故,但最新的方式是什么?在此先感谢
“我的实体对象返回了在我的”HomeViewModel“中找不到的特定类型,例如对象列表等等”这是什么意思? – gdoron
public ActionResult Index() var topTenList = repository.FindAllTopTen()。ToList(); return View(topTenList); } –
我返回了一个对象列表,但这只是我的一个部分视图,其他部分视图将需要其他数据,我将如何解决这个问题?我不能直接发送结果到我的偏好?只有我的主要观点我猜 –