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.
@ignatandrei: Of course, You are right that in this example I just return an empty view, but the instance "movie" is still empty. When I make the change You suggest, I still get an exception since the Categorie property is empty:
The model item passed into the dictionary is of type 'AE.Tps.Models.Movie', but this dictionary requires a model item of type 'AE.Tps.Models.Category'.
...and this is not the actual problem, the problem is that I can't get the data from the form to the controller.
@mbahn: Well, there is the data that one can fill in the form which I expect to be sent to the controller.
I still get an exception since the Categorie property is empty: The model item passed into the dictionary is of type 'AE.Tps.Models.Movie', but this dictionary requires a model item of type 'AE.Tps.Models.Category'
It is not because Category is empty - it is because you are passing to a View that have the model Category an Movie model.
It is not because Category is empty - it is because you are passing to a View that have the model Category an Movie model.
The exception message is misleading. The partial view _MoviePartial will send the model Movie to the partial view _CategoryPartial, since the Category is null. Probably because the first row in partial view _MoviePartial is @model AE.Tps.Models.Movie:
One little thing though, is that the framework automaticaly generates some stuff to the UI which I don't need, but I only need to create a custom editor for this model (I think).
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
mbhahn
Member
169 Points
119 Posts
Re: Submit data from a partial view within another partial view
May 11, 2012 03:13 PM|LINK
Do you have data some where?
ignatandrei
All-Star
135023 Points
21648 Posts
Moderator
MVP
Re: Submit data from a partial view within another partial view
May 11, 2012 04:04 PM|LINK
return View(movie);
Albertinho
0 Points
4 Posts
Re: Submit data from a partial view within another partial view
May 14, 2012 08:54 AM|LINK
@ignatandrei: Of course, You are right that in this example I just return an empty view, but the instance "movie" is still empty. When I make the change You suggest, I still get an exception since the Categorie property is empty: The model item passed into the dictionary is of type 'AE.Tps.Models.Movie', but this dictionary requires a model item of type 'AE.Tps.Models.Category'.
...and this is not the actual problem, the problem is that I can't get the data from the form to the controller.
@mbahn: Well, there is the data that one can fill in the form which I expect to be sent to the controller.
Is my approach to this totally wrong...?
Thanks for your replies!
ignatandrei
All-Star
135023 Points
21648 Posts
Moderator
MVP
Re: Submit data from a partial view within another partial view
May 14, 2012 09:00 AM|LINK
It is not because Category is empty - it is because you are passing to a View that have the model Category an Movie model.
Albertinho
0 Points
4 Posts
Re: Submit data from a partial view within another partial view
May 14, 2012 09:21 AM|LINK
The exception message is misleading. The partial view _MoviePartial will send the model Movie to the partial view _CategoryPartial, since the Category is null. Probably because the first row in partial view _MoviePartial is @model AE.Tps.Models.Movie:
@model AE.Tps.Models.Movie @Html.TextBoxFor(model => Model.MovieTitle) @Html.Partial("_CategoryPartial", Model.MovieCategory)So I think the problem still occurs before the controller returns the view. I must be doing wrong when I'm trying to get the input from a partial...
Albertinho
0 Points
4 Posts
Re: Submit data from a partial view within another partial view
May 14, 2012 09:58 AM|LINK
Finally, after hours spent...
I changed the view _MoviePartial:
@model AE.Tps.Models.Movie @Html.TextBoxFor(model => Model.MovieTitle) @Html.Partial("_CategoryPartial", Model.MovieCategory)...to this, using EditorFor instead of Partial:
...and it works!
One little thing though, is that the framework automaticaly generates some stuff to the UI which I don't need, but I only need to create a custom editor for this model (I think).
The main problem however is solved!
Thanks again for your replies!