我不知道为什么我的createviewmodel允许使用Id和PublisherId的版本,唯一可见的应该是Title,ImageLink和两个列表(可见,未显示)
不仅看起来不正确,而且在尝试创建对象时很可能还会产生错误,给我null引用错误
http://oi64.tinypic.com/2luwitj.jpg
我的模特
public class Game
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
public Publisher Publishers { get; set; }
public int PublisherID { get; set; }
public List<Category> Categories { get; set; }
public string ImageLink { get; set; }
public Game()
{
Categories = new List<Category>();
}
}
我的视图模型
public class GameViewModel
{
public int Id { get; set; }
public string Title { get; set; }
public object Category { get; set; }
public Publisher Publisher { get; set; }
public string ImageLink { get; set; }
public GameViewModel(int id, string title, List<Category> category, Publisher publisher, string imageLink)
{
this.ID = id;
this.Title = title;
this.Category = category;
this.Publisher = publisher;
this.ImageLink = imageLink;
}
CreateViewModel
public class CreateGameViewModel{
public Game Game { get; private set; }
public List<SelectListItem> Categories { get; private set; }
public List<SelectListItem> Publishers { get; private set; }
public int[] SelectedCategory { get; set; }
public CreateGameViewModel()
{
this.Game = new Game();
}
public CreateGameViewModel(List<Models.Category> categories, List<Models.Publisher> publishers) :this()
{
Categories = categories.Select(c => new SelectListItem() { Text = c.Name, Value = c.Id.ToString() }).ToList();
Publishers = publishers.Select(p => new SelectListItem() { Text = p.Name, Value = p.Id.ToString() }).ToList();
}
}
控制器
GET
public ActionResult Create()
{
var publishers = _db.Publishers.ToList();
var categories = _db.Categories.ToList();
var gameVM = new CreateGameViewModel(categories: categories, publishers: publishers);
return View(gameVM);
}
POST
[HttpPost]
public ActionResult Create(CreateGameViewModel game)
{
try
{
var categories = _db.Categories.Where(c => game.SelectedCategory.Contains(c.Id)).ToList();
game.Game.Categories = categories;
_db.Games.Add(game.Game);
_db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
然后创建视图
@model Project.ViewModels.CreateGameViewModel
<div class="row">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>CreateGameViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Game)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
<div class="col-xs-6">
<div class="row">
<div class="form-group">
<label class="control-label col-xs-3">Wydawcy</label>
<div class="col-xs-9">
@Html.DropDownListFor(model => model.Game.PublisherId, Model.Publishers ?? new List<SelectListItem>(), "-Please select-")
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<label class="control-label col-xs-3">Autorzy</label>
<div class="col-xs-9">
@Html.ListBoxFor(model => model.SelectedCategory, Model.Categories)
</div>
</div>
</div>