Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit multiple models in a single Razor View

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,BusinessDetails,Address,ServiceArea 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 the 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 have already made a 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?

like image 260
user1196392 Avatar asked May 23 '12 13:05

user1196392


People also ask

Can we use multiple models for single view?

You can use multiple models in a single view by creating a common model for all the models that are to be used in a single view. To achieve this, refer to the following steps. First, create a new model (common for all models) and refer all other models that are to be used in the same view.

How do you pass two models in a view?

You have to create a new model which has to contain the whole objects that you want to pass it to view. You should create a model (class, object) which inherits the base model (class, object).

Can one view have multiple controllers?

Yes, It is possible to share a view across multiple controllers by putting a view into the shared folder. By doing like this, you can automatically make the view available across multiple controllers.

What is the difference between Razor view and Razor page?

The difference between them is that View Pages are Razor views that are used to provide the HTML representations (aka views) for services in much the same way View Pages work for MVC Controllers.


2 Answers

You need to include the other ViewModels into a main CompositeModel like so

public class CompositeModel {
    public Album AlbumModel { get; set; }
    public Another AnotherModel { get; set; }
    public Other EvenMore { get; set; }
}

Send that to your view like so

public ActionResult Index() {
    var compositeModel = new CompositeModel();
    compositeModel.Album = new AlbumModel();
    compositeModel.OtherModel = new AnotherModel();
    compositeModel.EvenMore = new Other();        
    return View(compositeModel)
}

Modify your view to take the new model type

@model CompositeModel

To refer to properties of the sub-models you can use syntax like this

@Html.TextBoxFor(model => model.Album.ArtistName)

or you can create a view in the EditorTemplates folder that takes a sub-model like AlbumModel and use EditorFor like this

@Html.EditorFor(model => model.Album)

The template would look something like this

@model AlbumModel

@Html.TextBoxFor(model => model.AlbumName)
@Html.TextBoxFor(model => model.YearReleased)
@Html.TextBoxFor(model => model.ArtistName)

Now just post CompositeModel back to your controller and then save all the sub-models and now Bob's your uncle!

[HttpPost]
public ActionResult Index(CompositModel model) {
    // save all models
    // model.Album has all the AlbumModel properties
    // model.Another has the AnotherModel properties
    // model.EvenMore has the properties of Other
}
like image 144
CD Smith Avatar answered Nov 03 '22 10:11

CD Smith


You'll need to create a View Model that contains both of the types that you need. Something like this (assuming you're editing both an Album and an Artist):

public class MyModel
{
    public Album Album { get; set; }
    public Artist Artist { get; set; }
    public SelectList Genres { get; set; }
    public SelectList Artists{ get; set; }
}

Then change your view to use the new model like so:

@model MyModel

Then change your Get method to be something like:

public ActionResult Edit(int id) 
{
    var model = new MyModel();
    model.Album = db.Albums.Find(id);
    model.Artist = yourArtist; //whatever you want it to be
    model.Genres = new SelectList(db.Genres, "GenreId", "Name", model.Album.GenreId);
    model.Artists = new SelectList(db.Artists, "ArtistId", "Name", model.Album.ArtistId);

    return View(model);
}  

Then change your Post method to take the MyModel type:

[HttpPost]
public ActionResult Edit(MyModel model) {

    if (ModelState.IsValid) {
    //save your items here

        db.SaveChanges();

        return RedirectToAction("Index");
    }

    model.Genres = new SelectList(db.Genres, "GenreId", "Name", model.Album.GenreId);
    model.Artists = new SelectList(db.Artists, "ArtistId", "Name", model.Album.ArtistId);

    return View(album);
}      

Assuming your view has something like (wrapped in a form with a submit button of course):

@Html.EditorFor(m => m.Artist.Name) //do this for all Artist Fields
@Html.EditorFor(m =? m.Album.Name) //do this for all Album Fields

//the following two show you how to wire up your dropdowns:
@Html.DropDownListFor(m => m.Album.ArtistId, Model.Artists)
@Html.DropDownListFor(m => m.Album.GenreId, Model.Genres)
like image 45
mattytommo Avatar answered Nov 03 '22 08:11

mattytommo