use*_*392 10 .net c# asp.net-mvc razor asp.net-mvc-3
我是新来MVC3,我有一个多车型,如BussinessDetails,ContactPerson,ServiceArea,Address,还有更多车型.我有一个单一的视图页面,共享视图页面,如Contacts,BusinessDetails,Address,ServiceArea等.这些都在标签.他们有自己的模特.
我的问题是如何在同一个编辑视图页面中编辑多个模型.在发送这篇文章之前,我接受了MVC3"音乐商店"示例的帮助,但是只有一个模型ALBUM,如果有一个或多个模型我将在同一个视图页面中编辑,它们为一个模型提供编辑操作.
我已经创建了一个父业务规范类.这是来自MVC"音乐商店"
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);
}
Run Code Online (Sandbox Code Playgroud)
在HTTP POST只有型号ALBUM,如果有更多的型号如何,我多模型和视图进行编辑操作?
CD *_*ith 21
您需要将其他ViewModel包含在主体中CompositeModel,如此
public class CompositeModel {
public Album AlbumModel { get; set; }
public Another AnotherModel { get; set; }
public Other EvenMore { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
像这样发送到您的视图
public ActionResult Index() {
var compositeModel = new CompositeModel();
compositeModel.Album = new AlbumModel();
compositeModel.OtherModel = new AnotherModel();
compositeModel.EvenMore = new Other();
return View(compositeModel)
}
Run Code Online (Sandbox Code Playgroud)
修改视图以采用新模型类型
@model CompositeModel
Run Code Online (Sandbox Code Playgroud)
要引用子模型的属性,您可以使用这样的语法
@Html.TextBoxFor(model => model.Album.ArtistName)
Run Code Online (Sandbox Code Playgroud)
或者您可以在EditorTemplates采用子模型的文件夹中创建一个视图,AlbumModel并使用EditorFor这样的方法
@Html.EditorFor(model => model.Album)
Run Code Online (Sandbox Code Playgroud)
模板看起来像这样
@model AlbumModel
@Html.TextBoxFor(model => model.AlbumName)
@Html.TextBoxFor(model => model.YearReleased)
@Html.TextBoxFor(model => model.ArtistName)
Run Code Online (Sandbox Code Playgroud)
现在CompositeModel回到你的控制器,然后保存所有的子模型,现在鲍勃是你的叔叔!
[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
}
Run Code Online (Sandbox Code Playgroud)
您需要创建一个包含所需类型的视图模型.这样的事情(假设你正在编辑专辑和艺术家):
public class MyModel
{
public Album Album { get; set; }
public Artist Artist { get; set; }
public SelectList Genres { get; set; }
public SelectList Artists{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后更改您的视图以使用新模型,如下所示:
@model MyModel
Run Code Online (Sandbox Code Playgroud)
然后将您的Get方法更改为:
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);
}
Run Code Online (Sandbox Code Playgroud)
然后更改Post方法以获取MyModel类型:
[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);
}
Run Code Online (Sandbox Code Playgroud)
假设您的视图有类似的东西(包含在带有提交按钮的表单中):
@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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12660 次 |
| 最近记录: |