如何在单个 Razor 视图中编辑多个模型 [英] How to edit multiple models in a single Razor View

查看:42
本文介绍了如何在单个 Razor 视图中编辑多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 MVC3 的新手,我有多个模型,例如 BussinessDetailsContactPersonServiceAreaAddress以及更多型号.我有一个单一视图页面,其中共享视图页面,例如 ContactsBusinessDetailsAddressServiceArea 等.这些是全部在标签中.他们有自己的模型.

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.

我的问题是如何在同一个编辑视图页面中编辑多个模型.在发送这篇文章之前,我借助了 MVC3音乐商店"示例,但只有一个模型 ALBUM,如果有一个或多个模型,他们会为一个模型提供编辑操作,我将如何编辑相同的查看页面.

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.

我已经制作了一个父业务规范类.这是来自MVC音乐商店"

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);
}                                                                   

HTTP POST 中只有模型 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?

推荐答案

您需要像这样将其他 ViewModel 包含到主 CompositeModel

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; }
}

像这样发送到您的视图

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)

或者您可以在 EditorTemplates 文件夹中创建一个视图,它采用像 AlbumModel 这样的子模型,并像这样使用 EditorFor

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)

模板看起来像这样

@model AlbumModel

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

现在只需将 CompositeModel 发送回您的控制器,然后保存所有子模型,现在 Bob 是您的叔叔!

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
}

这篇关于如何在单个 Razor 视图中编辑多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆