Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 为什么我的ViewModel在[HttpPost]上为空。NETMVC3_Asp.net Mvc_Razor_Asp.net Mvc Viewmodel - Fatal编程技术网

Asp.net mvc 为什么我的ViewModel在[HttpPost]上为空。NETMVC3

Asp.net mvc 为什么我的ViewModel在[HttpPost]上为空。NETMVC3,asp.net-mvc,razor,asp.net-mvc-viewmodel,Asp.net Mvc,Razor,Asp.net Mvc Viewmodel,我正在尽最大努力在我的web应用程序中正确使用ViewModels,但我遇到了各种问题。其中之一是,如果我在使用Create操作发布后设置了断点,那么我的viewModel就没有存储任何表单值。我一定是做错了什么,但我试过几次。包括下面的代码,其中我将表单项命名为与viewModel字段相同的名称,以查看这是否有帮助 我还想知道viewmodel中到底应该表示哪些属性。我看到人们在博客帖子和诸如此类的文章中使用不同的东西 如果视图要呈现一个选择列表,我认为viewmodel应该为此保存一个IEn

我正在尽最大努力在我的web应用程序中正确使用ViewModels,但我遇到了各种问题。其中之一是,如果我在使用Create操作发布后设置了断点,那么我的viewModel就没有存储任何表单值。我一定是做错了什么,但我试过几次。包括下面的代码,其中我将表单项命名为与viewModel字段相同的名称,以查看这是否有帮助

我还想知道viewmodel中到底应该表示哪些属性。我看到人们在博客帖子和诸如此类的文章中使用不同的东西

如果视图要呈现一个选择列表,我认为viewmodel应该为此保存一个IEnumerable SelectListItem,如下所示。但我看到人们使用IEnumerable实体来表示选择列表所代表的类型

有人能帮我解释一下吗?昨晚我放弃了我的全部业务逻辑,这样我就可以重新开始,并尝试正确地完成它

我的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; }
}
我的看法是:

@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>
}   
@model GameserverCompare.ViewModels.Server.ServerCreateViewModel
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
服务器
@Html.HiddenFor(m=>m.Id)
@LabelFor(model=>model.CompanyName)
@DropDownListFor(m=>Model.CompanyName,Model.CompanyName)
@Html.ValidationMessageFor(model=>model.CompanyName)
@LabelFor(model=>model.GameTitle)
@DropDownListFor(m=>Model.GameTitle,Model.GameTitle)
@Html.ValidationMessageFor(model=>model.GameTitle)
@LabelFor(model=>model.City)
@DropDownListFor(m=>Model.City,Model.City)
@Html.ValidationMessageFor(model=>model.City)
@LabelFor(model=>model.NumberOfPlayers)
@DropDownListFor(m=>Model.NumberOfPlayers,Model.NumberOfPlayers)
@Html.ValidationMessageFor(model=>model.NumberOfPlayers)

}
由于您在表单模型中使用了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; }
}
让您的HttpPost操作将其中一个作为其参数


哦,请确保对
Id
属性使用
HiddenFor
,这样它会与其他数据一起发送回来。

谢谢,但这是否意味着我仍然需要使用Request.Form从POST获取值来填充ServerCreatePostViewModel?否,ServerCreatePostbackModel的属性应自动绑定到提供的表单值。您不必直接访问表单数据。我想我明白了,因此回发模型中的属性类型必须与表单将发布的类型相同。也就是说,下拉列表将返回一个字符串值,因此相应的属性也应该是一个字符串(即使实际上该字符串表示一个int引用Id,例如..公司)。因此,尽管viewmodel将捕获字符串,但我必须将其解析为int,以找到要引用的正确公司(或游戏,或其他)。谢谢最后一条评论,经过测试,效果良好。谢谢你帮我理解@CraigWhitley:实际上,如果属性是一个int,那么模型绑定器将足够聪明,可以自动将其转换为int(例如,在我发布的代码中的NumberOfPlayers上)。
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; }
}