【问题标题】:DropDownListFor select value based on value from databaseDropDownListFor 根据数据库中的值选择值
【发布时间】:2016-11-22 04:16:02
【问题描述】:

大家好。 我有下一个问题。

我有编辑表单,上面有几个 DropDownList 控件。 我需要根据数据库中的数据绑定 DropDownListFor 中的选定值。

接下来是我的:

型号:

供应商

public class Supplier{
    public int SupplierID { get; set; }       
    public string CompanyName { get; set; }
}

德尔维耶

public class Delivery{
    public int DeliveryID { get; set; }
    public string DeliveryCode { get; set; }
    public int SupplierID { get; set; }
    public virtual Supplier Supplier { get; set; }        
}

交付编辑屏幕的视图模型:

public class DeliveryEditViewModel{
    public int DeliveryID { get; set; }        
    public string SelectedCompanyName { get; set; }
    public IEnumerable<SelectListItem> Suppliers { get; set; }
}

查看:

<dd>
    @Html.DropDownListFor(x => x.SelectedCompanyName, new SelectList(Model.Suppliers, "Value", "Text"), htmlAttributes: new { @class = "form-control" })
</dd>

控制器:

 // GET: Deliveries/Edit
public ActionResult Edit(int? id){   
    db.Configuration.ProxyCreationEnabled = false;
        DeliveryEditViewModel model = db.Deliveries.Include(s => s.Supplier).Include(a => a.Auction).Include(q => q.Quality).Include(t => t.BulbType)
                .Where(d => d.DeliveryID == id)
                .Select(x => new DeliveryEditViewModel{
                    DeliveryID = x.DeliveryID,
                    SelectedCompanyName = x.Supplier.CompanyName
                })
                .Single();               
    model.Suppliers = db.Suppliers.Where(s => s.IsActive == true).Select(x => new SelectListItem{
        Value = x.SupplierID.ToString(),
        Text = x.CompanyName
    });
    return View(model);
}

问题是我无法根据数据库中已有的数据在 DropDownListFor 中设置选定的值。相反,我有 DropDown 控件,第一个元素始终处于活动状态。 我已经从Html.DropdownListFor selected value not being set 尝试过solutoin,但我仍然选择了第一个值而不是真正的值。

数小时的研究和已经大胆的头发让我停下来寻求帮助。 请帮我弄清楚这怎么可能。

非常感谢。

【问题讨论】:

  • Suppliers 已经是IEnumerable&lt;SelectListItem&gt; 为什么要在视图中使用new SelectList(Model.Suppliers, "Value", "Text") 创建另一个相同的对象?您需要设置SelectedCompanyName 的值以匹配其中一个选项(即SupplierIDSuppliers 表中的值之一(但您的代码建议该属性应该是int 而不是string
  • @StephenMuecke,非常感谢。我不知道我怎么能错过这个。现在它完美地工作了。你救了我)

标签: c# asp.net .net asp.net-mvc


【解决方案1】:

Suppliers 已经是IEnumerable&lt;SelectListItem&gt; 为什么要在视图中使用new SelectList(Model.Suppliers, "Value", "Text") 创建另一个相同的对象?您需要设置SelectedCompanyName 的值以匹配@StephenMuecke 表中SupplierID 的值之一(但您的代码建议属性应该是int 而不是string

【讨论】:

    【解决方案2】:

    我会建议您不要在控制器级别创建 SelectListItems。你可以做的是:

    public class DeliveryEditViewModel
    {
        public int DeliveryID { get; set; }        
        public string SelectedCompanyName { get; set; }
        public IEnumerable<Supplier> suppliers { get; set; }
    }
    

    控制器:

     // GET: Deliveries/Edit
    public ActionResult Edit(int? id){   
        db.Configuration.ProxyCreationEnabled = false;
            DeliveryEditViewModel model = db.Deliveries.Include(s => s.Supplier).Include(a => a.Auction).Include(q => q.Quality).Include(t => t.BulbType)
                    .Where(d => d.DeliveryID == id)
                    .Select(x => new DeliveryEditViewModel{
                        DeliveryID = x.DeliveryID,
                        SelectedCompanyName = x.Supplier.CompanyName
                    })
                    .Single();               
        Model.suppliers = db.Suppliers.Where(s => s.IsActive == true);
        return View(model);
    }
    

    查看:

      @Html.DropDownListFor(model => model.SelectedCompanyName, ((IEnumerable<Supplier>)Model.supplierss).Select(x => new SelectListItem()
               {
                   Text = x.CompanyName.ToString(),
                   Value = x.SupplierID.ToString(),
                   Selected = (Model != null) && (x.CompanyName == Model.SelectedCompanyName)
               }), "-- Select Supplier --")
    

    这是一个非常有用的技巧,您可以更改 Text 或 Value 或 Selected Value 或 Default Selected 选项,而无需更改控制器并重新构建以反映。

    【讨论】:

    • 我试图避免使用 ViewBags。老实说,现在我看不出使用 ViewBag 有什么好处。也许我错过了什么。不过还是谢谢你的建议。
    • @ЖенькаСидоров 我有,编辑了我的帖子。请快速浏览
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    相关资源
    最近更新 更多