Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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
C# 未在SelectList的下拉列表中设置选定值_C#_Asp.net_Asp.net Mvc 3_Asp.net Mvc 4_Razor - Fatal编程技术网

C# 未在SelectList的下拉列表中设置选定值

C# 未在SelectList的下拉列表中设置选定值,c#,asp.net,asp.net-mvc-3,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc 3,Asp.net Mvc 4,Razor,MVC中的下拉列表…基本上是我存在的祸根 我已经看到了很多关于这个问题的话题,但是我在这里和互联网上找到的所有东西都没有解决我的问题 首先介绍一下背景知识: 我有一个允许用户编辑记录的页面。编辑页面上的其中一个字段是一个下拉列表,其中充满了客户端。要进入编辑页面,用户单击另一个页面上的编辑链接,该页面上有一个记录表。当他们进入编辑页面时,表单将填充所选记录中要编辑的信息。除了登录屏幕,它可能是任何移动数据的网站最基本和最常见的功能之一 下面是视图中的代码: @Html.DropDownList(

MVC中的下拉列表…基本上是我存在的祸根

我已经看到了很多关于这个问题的话题,但是我在这里和互联网上找到的所有东西都没有解决我的问题

首先介绍一下背景知识: 我有一个允许用户编辑记录的页面。编辑页面上的其中一个字段是一个下拉列表,其中充满了客户端。要进入编辑页面,用户单击另一个页面上的编辑链接,该页面上有一个记录表。当他们进入编辑页面时,表单将填充所选记录中要编辑的信息。除了登录屏幕,它可能是任何移动数据的网站最基本和最常见的功能之一

下面是视图中的代码:

@Html.DropDownList("ClientsDropdown", (IEnumerable<SelectListItem>)ViewBag.ClientsDropdown, "Select a Client")
和FillClientDropdown(发票)调用:

我已经确认clientList确实从数据库中获得了一个很好的客户机列表,并且正确的SelectListItem将“Selected”属性设置为True

但当我加载页面并查看下拉列表时,我得到的是:

<select id="ClientsDropdown" name="ClientsDropdown">
<option value="">Select a Client</option>
<option value="1">Test Client 1</option>
<option value="2">Test Client 2</option>
然后,在我的InvoiceController中,我稍微更改了我的详细信息:

[HttpGet]
    public ViewResult Details(int id, int pageNumber = 1)
    {
        Invoice invoice = db.Invoices.Find(id);
        var clientList = db
            .Clients
            .OrderBy(x => x.Name)
            .ToList()
            .Select(x => new SelectListItem
            {
                Text = x.Name,
                Value = x.ID.ToString()
            });
        var model = new InvoiceDetailsModel() { Invoice = invoice, PageNumber = pageNumber };
        model.ClientList = new SelectList(clientList, "Value", "Text", model.Invoice.ClientOcrStringID);
        return View(model);
    }
利用我添加到InvoiceDetailsModel的优势。最后,在我看来,我将下拉列表改为:

@Html.DropDownListFor(model => model.Invoice.ClientOcrStringID, Model.ClientList, "Select a Client")

我希望这对将来的任何人都有帮助。

重构代码,避免使用ViewBag或ViewData。这听起来很傻,但它会工作,你会有一个更好的代码

你必须做的是:

  • 创建一个ViewModel类,它可以从Invoice类派生
  • 添加附加属性:pageNumber和SelectList
  • 在视图中,使用DropDownListFor辅助对象

我已编辑了您的标题。请看“”,其中的共识是“不,他们不应该”。哦,很抱歉!嗯,我正在编写别人的代码,所以我没有机会重写很多代码。但是,我发现他们确实有一个ViewModel类,我可以使用它。我遇到的问题是如何设置SelectedValue属性。
public SelectList ClientList { get; set; }
[HttpGet]
    public ViewResult Details(int id, int pageNumber = 1)
    {
        Invoice invoice = db.Invoices.Find(id);
        var clientList = db
            .Clients
            .OrderBy(x => x.Name)
            .ToList()
            .Select(x => new SelectListItem
            {
                Text = x.Name,
                Value = x.ID.ToString()
            });
        var model = new InvoiceDetailsModel() { Invoice = invoice, PageNumber = pageNumber };
        model.ClientList = new SelectList(clientList, "Value", "Text", model.Invoice.ClientOcrStringID);
        return View(model);
    }
@Html.DropDownListFor(model => model.Invoice.ClientOcrStringID, Model.ClientList, "Select a Client")