user1196392
user1196392

Reputation: 199

How to edit two or more models in a single view page

I am new to MVC3, I have an multiple models like BussinessDetails,ContactPerson,servicearea,address and many more models. i have a single view page where shared view pages like contacts,bussinessdetails,address,service area etc.these are all in tabs.they have there own models.

My problem is that how to edit multiple models in a same edit view page.Before sending this post i take the help of MVC3 "Music Store" Example but there is only one model "ALBUM" and they give edit operation for one model if there is one or more model how I shall edit in the same view page.

i already make parent business specification class.this is from MVC "Music Store"

 public ActionResult Edit(int id) {
   Album album = db.Albums.Find(id);
   ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
   ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", 
                                                             album.ArtistId);

   return View(album);
 }                                                        


[HttpPost]
public ActionResult Edit(Album album) {

  if (ModelState.IsValid) {
     db.Entry(album).State = EntityState.Modified;
     db.SaveChanges();
     return RedirectToAction("Index");
  }

  ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
  ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", 
                                                             album.ArtistId);

  return View(album);
}                                                                   

In HTTP POST there is only on model "ALBUM" if there is more models how i am perform edit operation on multiple models and view?

Upvotes: 0

Views: 224

Answers (2)

Tanveer
Tanveer

Reputation: 2087

yes you can do it by using view models , please have a look on my answer in this post

Requred field validate MVC

Upvotes: 0

COLD TOLD
COLD TOLD

Reputation: 13569

create a new model that would contain the information about two or more models such as

public class MainPageModel{
    public mod mod{get; set;}
    public mod2 mod2{get; set;}
}

and the in your action reference the MainPageModel and just edit on the object return the model to which the object belongs

here is a good tutorial

http://francorobles.wordpress.com/2011/05/03/returning-multiple-models-in-1-view-in-asp-net-mvc3/

Upvotes: 2

Related Questions