[asp.net]在创建返回转储中创建下拉列表

发布时间: 2016/7/19 3:16:13
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我已经创建 MVC5 应用和我想要在创建使用下拉列表中的服务器属性和我得到转储 (错误是:"对象未引用设置到对象的实例") 在运行它时如何应该做这种权利?

我试着用

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

                @Html.DropDownListFor(model => model.SystemType, Model.SystemType)
            </div>
    </div>

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

在模型类中我有下面的代码

公共类 Amta {

    public string User { get; set; }

    public IEnumerable<SelectListItem> SystemType
    {
        get
        {
            return new[]
            {
                new SelectListItem {Value = "D", Text = "Dev"},
                new SelectListItem {Value = "p", Text = "Production"}
            };
        }
    }
}

解决方法 1:

将属性添加到 Amta 模型中,以便收集所选的值,当窗体回发。

@Html.DropDownList("SelectedSystemType", Model.SystemType)

@Html.DropDownListFor(model => model.SelectedSystemType, Model.SystemType)

public class Amta {

    public string User { get; set; }

    public string SelectedSystemType { get; set; }

    public IEnumerable<SelectListItem> SystemType
    {
        get
        {
            return new[]
            {
                new SelectListItem {Value = "D", Text = "Dev"},
                new SelectListItem {Value = "p", Text = "Production"}
            };
        }
    }
}

# # 这里是我的测试 # #

控制器

public ActionResult Test()
{
    return View(new TestModel());
}

[HttpPost]
public ActionResult Test(TestModel model)
{
    return View(model);
}

视图

@model DemoKendoMvc.Models.TestModel
@{
    ViewBag.Title = "Test";
}

@using (Html.BeginForm("Test", "Home", FormMethod.Post))
{
    @Html.DropDownListFor(model => model.SelectedSystemType, Model.SystemType)
    @Html.EditorFor(model => model.User)
    @Html.ValidationMessageFor(model => model.User)
    <input type="submit" value="Submit" />
}

模型

public class TestModel
{
    public string User { get; set; }

    public string SelectedSystemType { get; set; }

    public IEnumerable<SelectListItem> SystemType
    {
        get
        {
            return new[]
            {
                new SelectListItem {Value = "D", Text = "Dev"},
                new SelectListItem {Value = "p", Text = "Production"}
            };
        }
    }
}

已过帐的值

enter image description here

# # 更新要求在注释的代码 # #

public ActionResult Create()
{
    return View(new TestModel());
}

[HttpPost]
public ActionResult Create(TestModel model)
{
    try
    {
        // TODO: Add insert logic here

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

如果您在创建视图时使用创建模板SelectedSystemType被创建为EditorFor。因此,你需要将其更改为DropDownListFor

@Html.DropDownListFor(model => model.SelectedSystemType, Model.SystemType)
赞助商