为什么我的ViewModel在[HttpPost]上为空?.NET MVC 3

Kia*_*ada 5 asp.net-mvc razor asp.net-mvc-viewmodel

我正努力在我的Web应用程序中正确使用ViewModels,但我遇到了各种各样的问题.其中之一是,如果我在使用Create操作发布后立即设置断点,则我的viewModel没有存储任何表单值.我一定做错了什么,但我尝试了一些事情.包括下面的代码,我将表单项命名为与viewModel字段相同,以查看是否有帮助.

我也想知道你的viewmodel中应该代表什么属性.我见过人们在博文中使用不同的东西和诸如此类的东西.

如果视图将呈现一个选择列表,那么我认为viewmodel应该为此保存一个IEnumerable SelectListItem,如下所示.然而,我看到人们使用IEnumerable Entity来表示选择列表所代表的类型.

任何人都可以为我阐明这一点吗?我昨晚取消了整个业务逻辑,所以我可以重新开始尝试并正确地完成它.

我的ViewModel:

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; }
}
Run Code Online (Sandbox Code Playgroud)

我的控制器动作:

    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");
        }
    }  
Run Code Online (Sandbox Code Playgroud)

我的看法:

@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>
}   
Run Code Online (Sandbox Code Playgroud)

Str*_*ior 4

由于您在表单模型中使用 SelectList 属性,因此您需要有一个不同的模型来表示这些列表中的选定值:

public class ServerCreatePostbackModel
{
    public int Id { get; set; }

    // CompanyName represents a field in the Company model. 
    public string CompanyName { get; set; }

    // Represents the Game model.
    public string GameTitle { get; set; }

    //Represents the Location model, etc...
    public string City { get; set; }
    public int NumberOfPlayers { get; set; }
    public string CurrencyAbbreviation { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

让您的 HttpPost 操作将其中之一作为其参数。

哦,一定要使用HiddenForId属性,这样它就会与其他数据一起发送回来。