Binding model to view issue - asp.net-mvc-3

I have my own model classes, which are public, but when making a view, I cannot find the model itself in the list of models to bind to. Why is this? Can I bind the model to the view somehow afterwards?
Thanks

Add the following to the top of your View:
#model your.namespace.YourModelClass
Then your controllers' action method needs to return an appropriate instance of YourModelClass:
public ActionResult Index(){
return View(new YourModelClass());
}

Related

MVC3 System.Collections.Generic.List pass the wrong model item

Hello everyone I have checked my controller and view it seems there is no problem but I get System Colletion error.
Here is my controller
public ViewResult Index()
{
return View(db.banner.ToList());
}
Here is My View
{
#model IEnumerable<icerik.Models.banner>
}
And I get this error
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[icerik.Models.banner]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[icerik.Models.contents]'.
Maybe you have a partial in your main view:
#Html.Partial("SomePartial")
and this partial is strongly typed to IEnumerable<contents>:
#model IEnumerable<icerik.Models.contents>
So make sure that you are passing the correct model to this partial. If you do not specify anything to the Partial helper (as in my example) the main model will be passed to this partial.
So always specify the correct model:
#Html.Partial("SomePartial", SomeModelInstance)

call Model function from View

I have an HTML select in View, for the select options i have to retrieve value from database. I have a function in model that returns LIST of options. How can I call the model's function from view.
Something like this:
public class XXXXViewModel {
//properties...
public List<Option> Options {get;set;}
}
and in your controller:
public class XXXXController : Controller {
public ActionResult SomeAction(){
var model = GetModelFromRepository();
var viewModel = new XXXXViewModel{
//Properties...
Options = model.GetOptions();
};
return View(viewModel);
}
}
So, you controller takes care of providing the options to the view using a ViewModel class containing everything your view needs.
Hope it helps.
create an event which is going to call that function and by the help of json and Ajax javascript you can easily do it
its a small concept that model canot be call from html after rendering on client side you have to take care of it by controller action and ajax is a good practice for it in maximum of mvc application

Passing model from view to controller

After Learning Jon Galloways MVC Music Store Example.I Just didn't understood the create view How to pass model to controller in which we could see it from parameter in the action Create(Movie movie). Thanks.
[HttpPost]
public ActionResult Create(Movie movie)
{
if (ModelState.IsValid)
{
db.Movies.Add(movie);//Where is the movie come from?
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
In the code example you have posted, the Movie model will be created through model binding. During this process any of your form variables will be matched up with the object specified in the action.
For instance the value of
<input type="text" name="Title"/>
would be assigned to movie's Title property.
A view can be associated with a model by declaring (Razor syntax)
#model GallowaySample.Movie
Normally you do not pass the model to the controller, but you create an instance of your model in your controller.

Return a view from Post method

I have this post method:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Invitations(SuperInvitationsEditModel model)
{
...
var newmodel = new SuperInvitationsEditModel();
if (hasErrors)
{
SuperInvitationsErrorModel newErrorModel = new SuperInvitationsErrorModel();
newErrorModel.Errors = model.Errors;
return View(newErrorModel);
}
return View(newmodel);
}
When this code in the if(hasErrors) executes I get this error.
The model item passed into the dictionary is of type 'MyProject.Models.SuperInvitationsErrorModel', but this dictionary requires a model item of type 'MyProject.Models.SuperInvitationsEditModel'.
I thought I can do this since the return value of the method is a generic ActionResult. Can anyone tell me why is this not working?
because your current view is strongly typed. change the code as
return View("yourviewname",newErrorModel);
It has nothing to do with casting ViewResult to ActionResult. The problem is, that you have strongly typed view that expects the model of type SuperInvitationsEditModel (see #model on the top of Invitations.cshtml), but you are passing the model of type SuperInvitationsErrorModel to it.
You should merge the two view model classes (SuperInvitationsEditModel and SuperInvitationsErrorModel) into one, or create a standalone view for each of them.

Pass viewmodel to ajax controller action that returns partial with that model?

I have a view that is strongly typed. Inside this view i have jqueryui tabs, that when clicked call my Controller and return a partial view
("#tab0").load('#Url.Action("ProfileImage", "User")');
public ActionResult ProfileImage()
{
return PartialView("_ProfileImage");
}
What I'd like to do is pass the model from the "parent" view to the controller which can then bind it to the partial when it is returned:
("#tab0").load('#Url.Action("ProfileImage", "User", new {model=model})');
public ActionResult ProfileImage(UserViewModel model)
{
return PartialView("_ProfileImage", model);
}
Is this possible? how is this normally done? Where you have the model data in one view and you'd like to pass it to a asynchronously loaded partial view?
You could create a ToJson method on your viewmodel, which could be something like this:
public IHtmlString ToJson()
{
return MvcHtmlString.Create(Json.Encode(this));
}
It just serializes the viewmodel into a json. The IHtmlString returntype makes sure the output isn't encoded in your view.
The call to your controller would be something like this:
("#tab0").load('#Url.Action("ProfileImage", "User", new {model=model.ToJson()})');
The json modelbinder can recreate the viewmodel on the serverside. You probably run into some problems along the way, but nothing unsolvable I guess.

Resources