asp.net-mvc ViewModel 中的 ASP.NET MVC SelectList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20387205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:22:13  来源:igfitidea点击:

ASP.NET MVC SelectList in ViewModel

asp.net-mvcasp.net-mvc-4

提问by carlg

I'm working in ASP.NET MVC 5 (but this most likely applies to previous versions also). Best way to ask this question is to show you the code:

我在 ASP.NET MVC 5 中工作(但这很可能也适用于以前的版本)。问这个问题的最佳方法是向您展示代码:

Here is the View Model:

这是视图模型:

public class PersonCreateViewModel
{
    public SelectList cities {get; set;}
    public String Name { get; set; }
    public String Address { get; set; }

}

Here is the http Post method from the controller:

这是来自控制器的 http Post 方法:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(PersonCreateViewModel viewmodel)
{

    if (ModelState.IsValid)
    {
        //Add to database here and return

    }

    //return back to view if invalid db save
    return View(person);
}

Here is the View:

这是视图:

<div class="form-group">
        @Html.LabelFor(model => model.person.name, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
        @Html.EditorFor(model => model.person.name)
        @Html.ValidationMessageFor(model => model.person.name)
    </div>
</div>

<div class="form-group">
        @Html.LabelFor(model => model.person.address, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
        @Html.EditorFor(model => model.person.address)
        @Html.ValidationMessageFor(model => model.person.address)
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(model => model.person.CityID, "CityID", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("cities")
        @Html.ValidationMessageFor(model => model.person.CityID)
    </div>
</div>

When the user clicks submit, the following error message is in the browser: "No parameterless constructor defined for this object. "

当用户单击提交时,浏览器中会出现以下错误消息:“没有为此对象定义无参数构造函数。”

I think it has something to do with the fact that I have a SelectList in my ViewModel. I think when the view passes the model back to the controller on form submission, it calls the constructor for the SelectList, but there is no parameterless constructor for SelectList. I'm not sure how to proceed. Any help is appreciated!!

我认为这与我的 ViewModel 中有一个 SelectList 的事实有关。我认为当视图在表单提交时将模型传递回控制器时,它会调用 SelectList 的构造函数,但是 SelectList 没有无参数的构造函数。我不知道如何继续。任何帮助表示赞赏!

回答by Mark Olsen

I've always had better luck using IEnumerable

使用 IEnumerable 我总是有更好的运气

public class PersonCreateViewModel
{
    public IEnumerable<SelectListItem> cities {get; set;}
    public int CityId { get; set; }
    public String Name { get; set; }
    public String Address { get; set; }
}

Also, you are going to need a property on the view model to capture the selected value like CityId.

此外,您将需要视图模型上的一个属性来捕获选定的值,如 CityId。

Then you can use:

然后你可以使用:

Html.DropDownListFor(m => m.CityId, Model.cities)