对象引用未设置为模型中下拉列表的对象实例

时间:2012-12-14 15:12:49

标签: asp.net-mvc razor

我不知道为什么会这样,我已经设置了值并对其进行了调试,但它并没有将信息从控制器传递给视图。这是正在发生的事情

型号:

public class QueueFilterModel
{
    public string SelectedFilter { get; set; }
    public string query { get; set; }
    public List<string> MyFilterList { get; set; } 


}

控制器:

[HttpGet]
    public ActionResult Queue()
    {
        QueueFilterModel model = new QueueFilterModel()
        {
            SelectedFilter = "All",
            query = "SELECT * FROM [CHAVI].[dbo].[TicketQueue]",
            MyFilterList = new List<string>()

        };
        model.MyFilterList.Add("All");
        model.MyFilterList.Add("Open");
        model.MyFilterList.Add("Closed");


        return View();
    }

查看:

@model RazorARPP.Models.QueueFilterModel
@{
ViewBag.Title = "Queue";
}

<h2>Queue</h2>


    <form action="" method="post" enctype="multipart/form-data" id="MyForm">

        Filter
        <div>
            Filter Options:
        </div>
        <div>
            @Html.DropDownList("test", new SelectList(Model.MyFilterList,Model.SelectedFilter))
        </div>


            <h3>Insert Instructions Here</h3>
                @{
                    var DB = Database.Open("CHAVI");
                    var grid = new WebGrid(DB.Query("SELECT * FROM [TicketQueue]"), null, null, 20);

                    @grid.GetHtml(
                        tableStyle: "webgrid",
                        columns: grid.Columns(
                        grid.Column(header: "Link", style: "labelcolumn", format: (item) => Html.ActionLink("Edit Item", "EditQueue", new { id = item.QueueID})),
                        grid.Column("Description", "Description"),
                        grid.Column("QueueDate", "QueueDate"),
                        grid.Column("Note", "Note"),
                        grid.Column("Status", "Status"),
                        grid.Column("LastUpdated", "LastUpdated")
                            )
                    )
                 }
    </form>

网格部分工作正常(和查询)。问题在于下拉列表,它没有设置任何东西。有什么想法吗?谢谢。

2 个答案:

答案 0 :(得分:1)

您是否未通过该模型进行查看?

不应该

public ActionResult Queue()
    {
        QueueFilterModel model = new QueueFilterModel()
        {
            SelectedFilter = "All",
            query = "SELECT * FROM [CHAVI].[dbo].[TicketQueue]",
            MyFilterList = new List<string>()

        };
        model.MyFilterList.Add("All");
        model.MyFilterList.Add("Open");
        model.MyFilterList.Add("Closed");


        return View(model);
    }

答案 1 :(得分:0)

尝试使用: -

public ActionResult Queue()
{
    QueueFilterModel model = new QueueFilterModel()
    {
        SelectedFilter = "All",
        query = "SELECT * FROM [CHAVI].[dbo].[TicketQueue]",
        MyFilterList = new List<string>()

    };
    model.MyFilterList.Add("All");
    model.MyFilterList.Add("Open");
    model.MyFilterList.Add("Closed");


    return View(model);
}