Let me explain my problem:
I have four tables created as objects using Entity Framework.
I have added a repository class to the Entity Model to add/remove/get/query stuff that I need.
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;
}
}
and so on…
My Index View is divided into some partial Views such as:
@{
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")
let say that in my _PartialOtherView i have a form where i want to type in some info and add it to a database:
@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>
This request goes to controller:
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");
}
}
how would I solve the issue of passing the right model to my index View when all of my partialviews would be dealing with different models.. i’ve tried to create a class that encapsulates all my models, but this just created further problems because my entity object returned particular types not found in my “HomeViewModel” such as list of objects and so on
this really confuses me, how would I resolve this, i’m sure it can be done somehow, but whats the right way? thanks in advance
I think (from my understanding of the problem) that what you need is to pass a view model to the index view containing any further models such as:
Then in the Index() action you would return the view model:
Then the view would pass this model to/from the partial view as:
The submission of the form in the partial view should call the AddTopTenFav() and correctly pass the TopTenFav model to the action.