我正在尽最大努力在我的 Web 应用程序中正确使用 ViewModel,但我遇到了各种问题。其中之一是,如果我在使用 Create 操作发布后设置断点,我的 viewModel 没有存储我的任何表单值。我一定做错了什么,但我已经尝试了一些事情。包括下面的代码,我将表单项命名为与 viewModel 字段相同的名称,以查看是否有帮助。
我还想知道您的视图模型中的确切属性应该代表什么。我见过人们在博客文章中使用不同的东西等等。
如果视图要呈现一个选择列表,我的印象是视图模型应该为此保存一个 IEnumerable SelectListItem,如下所示。然而,我看到人们使用 IEnumerable Entity 来表示选择列表所代表的类型。
有人可以为我解释一下吗?昨晚我放弃了我的整个业务逻辑,所以我可以重新开始并尝试正确地做到这一点。
我的视图模型:
public class ServerCreateViewModel
{
public int Id { get; set; }
// CompanyName represents a field in the Company model. I did this to see if
// it would help with model binding. Beforehand it was Companies to represent the type. I've done the same for the rest of them, so I wont comment on this again.
public IEnumerable<SelectListItem> CompanyName { get; set; }
// Represents the Game model.
public IEnumerable<SelectListItem> GameTitle { get; set; }
//Represents the Location model, etc...
public IEnumerable<SelectListItem> City { get; set; }
public IEnumerable<SelectListItem> NumberOfPlayers { get; set; }
public IEnumerable<SelectListItem> CurrencyAbbreviation { get; set; }
}
我的控制器动作:
public ActionResult Create()
{
var viewModel = new ServerCreateViewModel();
viewModel.CompanyName = new SelectList(_dataService.Companies.All(), "Id", "CompanyName");
viewModel.GameTitle = new SelectList(_dataService.Games.All(), "Id", "GameTitle");
viewModel.City = new SelectList(_dataService.Locations.All(), "Id", "City");
viewModel.NumberOfPlayers = new SelectList(_dataService.ServerPlayers.All(), "Id", "NumberOfPlayers");
return View(viewModel);
}
[HttpPost]
public ActionResult Create(FormCollection collection, ServerCreateViewModel viewModel)
{
try
{ // I put a breakpoint in here to check the viewModel values.
// If I dont pass the viewModel into the constructor, it doesnt exist.
// When I do pass it in, its empty.
return Content("Success");
}
catch
{
return Content("Fail");
}
}
我的观点:
@model GameserverCompare.ViewModels.Server.ServerCreateViewModel
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Server</legend>
@Html.HiddenFor(m => m.Id)
<div class="editor-label">
@Html.LabelFor(model => model.CompanyName)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => Model.CompanyName, Model.CompanyName)
@Html.ValidationMessageFor(model => model.CompanyName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.GameTitle)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => Model.GameTitle, Model.GameTitle)
@Html.ValidationMessageFor(model => model.GameTitle)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => Model.City, Model.City)
@Html.ValidationMessageFor(model => model.City)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NumberOfPlayers)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => Model.NumberOfPlayers, Model.NumberOfPlayers)
@Html.ValidationMessageFor(model => model.NumberOfPlayers)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}