I'm new to MVC3 and I'm looking for a web user control equivalent, I've been told that partial view is the way to go.
Now I'm trying to retrieve data from a partial view to submit it with a form.
I have two models, and one is the property of the other:
namespace AE.Tps.Models
{
public class Movie
{
public string MovieTitle { get; set; }
public Category MovieCategory { get; set; }
}
public class Category
{
public bool Action { get; set; }
public bool Comedy { get; set; }
public bool Thriller { get; set; }
public bool Drama { get; set; }
public bool SciFi { get; set; }
public bool Horror { get; set; }
}
}
The controller creates a default model for the starting page (Index) and also handles the HttpPost:
namespace AE.Tps.Controllers
{
public class MovieController : Controller
{
public ActionResult Index()
{
Movie defaultMovie = new Movie() { MovieCategory = new Category(), MovieTitle = string.Empty };
return View(defaultMovie);
}
[HttpPost]
public ActionResult Index(Movie movie)
{
return View();
}
}
}
The view takes the model Movie and creates a form:
When I run the app, I get the exception : Object reference not set to an instance of an object.
I add a breakpoint in the controller, at the second Index method (HttpPost), to see the Movie data but I can only get the title, the categories I've checked are gone and the Category = null.
Do I have to create a new model which includes both models and pass that on to the partial view? But what if I want to use only the _CategoryPartial on another view, then I would need to give it more data than necessary.
Albertinho
0 Points
4 Posts
Submit data from a partial view within another partial view
May 11, 2012 10:08 AM|LINK
Hi!
I'm new to MVC3 and I'm looking for a web user control equivalent, I've been told that partial view is the way to go.
Now I'm trying to retrieve data from a partial view to submit it with a form.
I have two models, and one is the property of the other:
namespace AE.Tps.Models { public class Movie { public string MovieTitle { get; set; } public Category MovieCategory { get; set; } } public class Category { public bool Action { get; set; } public bool Comedy { get; set; } public bool Thriller { get; set; } public bool Drama { get; set; } public bool SciFi { get; set; } public bool Horror { get; set; } } }The controller creates a default model for the starting page (Index) and also handles the HttpPost:
namespace AE.Tps.Controllers { public class MovieController : Controller { public ActionResult Index() { Movie defaultMovie = new Movie() { MovieCategory = new Category(), MovieTitle = string.Empty }; return View(defaultMovie); } [HttpPost] public ActionResult Index(Movie movie) { return View(); } } }The view takes the model Movie and creates a form:
@model AE.Tps.Models.Movie @using (Html.BeginForm()) { @Html.Partial("_MoviePartial", Model) <input type="submit" /> }Under the Views/Shared folder, I have added two partial views, _MoviePartial:
@model AE.Tps.Models.Movie @Html.TextBoxFor(model => Model.MovieTitle) @Html.Partial("_CategoryPartial", Model.MovieCategory)...and _CategoryPartial:
When I run the app, I get the exception : Object reference not set to an instance of an object.
I add a breakpoint in the controller, at the second Index method (HttpPost), to see the Movie data but I can only get the title, the categories I've checked are gone and the Category = null.
Do I have to create a new model which includes both models and pass that on to the partial view? But what if I want to use only the _CategoryPartial on another view, then I would need to give it more data than necessary.
Submit PartialView HttpPost mvc3