Asp.net 在创建返回转储中创建下拉列表

Asp.net 在创建返回转储中创建下拉列表,asp.net,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,razor,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,Razor,我已经创建了MVC5应用程序,我希望在服务器属性的创建使用下拉列表中 当我运行它时,我得到dump(错误是:“对象引用未设置为对象的实例”) 我试着用 <div class="form-group"> @Html.LabelFor(model => model.SystemType, new { @class = "control-label col-md-2" }) <div class="col-md-10">

我已经创建了MVC5应用程序,我希望在服务器属性的创建使用下拉列表中 当我运行它时,我得到dump(错误是:“对象引用未设置为对象的实例”)

我试着用

    <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>

@LabelFor(model=>model.SystemType,新的{@class=“controllabel col-md-2”})
@Html.DropDownListFor(model=>model.SystemType,model.SystemType)
@LabelFor(model=>model.User,新的{@class=“controllabel col-md-2”})
@EditorFor(model=>model.User)
@Html.ValidationMessageFor(model=>model.User)
在模型类中,我有以下代码

公共类Amta {

公共字符串用户{get;set;}
公共IEnumerable系统类型
{
得到
{
返回新的[]
{
新建SelectListItem{Value=“D”,Text=“Dev”},
新建SelectListItem{Value=“p”,Text=“Production”}
};
}
}
}

检查DropDownListFor的重载。我觉得你需要用这个

第一个表达式应该是lambda表达式,它指向字符串返回类型的属性。这将返回下拉列表的选定元素

第二个表达式应该是要与下拉列表绑定的列表

检查这两件事,让我知道,如果它仍然不适合你


在这里,您正在使用lambda表达式中的
SystemType
属性以及列表。

向Amta模型添加属性,以便在表单发回时收集所选值

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

看法 如果在创建视图时使用Create TemplateSelectedSystemType将被创建为editor for。因此,您需要将其更改为DropDownListFor

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

Model.Server需要是一个selectList什么是“Model.Server”?@COLDTOLD-这是错误的,因为我做了所有的测试后都改变了。。我更新了帖子,还是没有working@RKS-错误,我更新的帖子仍然不起作用Anks您能否提供示例,说明如何将列表放在那里,因为这是在模型上…正如您在SystemType上面定义了一个属性用户,您需要定义一个更多的属性,该属性将在发布期间携带所选的值。你需要在lambda Expression中绑定此属性用户正在工作,但这里我需要下拉列表,你能发布你认为应该解决问题的代码吗?这正是Win在回复中发布的方式。谢谢tim,我尝试了这两种方法,但仍然得到相同的转储,我担心这是因为第二个参数Model.SystemType,知道吗?即使我添加@if(Model.SystemType!=null){@Html.DropDownList(“SelectedSystemType”,Model.SystemType)}我也不明白,我创建了一个新项目,创建类并在编写时放置属性,然后通过scaffold创建新的控制器,并将下拉列表的代码放置在创建操作中,而该操作不起作用。当我把代码放在索引cshtml中时,下拉列表正在运行。问题是什么?默认情况下,ASP.NETMVC模板附带了很多额外的东西。最坏的情况是,您可以尝试使用空的MVC模板。是的,但我需要该表,因为我有绑定到sql表的附加属性…也许在创建中,我应该使用不同的代码作为下拉列表?在您的程序中,您生成脚手架并将代码放入创建或索引中?在索引中,我可以看到下拉列表,但在创建中,我使用了相同的代码,我得到了转储。。。
@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"}
            };
        }
    }
}
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();
    }
}
@Html.DropDownListFor(model => model.SelectedSystemType, Model.SystemType)