【问题标题】:MVC 3 DropDownList displays multiple values on Edit pageMVC 3 DropDownList 在编辑页面上显示多个值
【发布时间】:2012-11-11 06:16:18
【问题描述】:

我正在使用枚举来填充我的 DropDownList,它可以与 List 和 Create 视图一起正常工作,但我的 Edit 视图会在 DropDownList 中加载重复值。重复的值只是属性含义的选定值,如果 Rented 是保存在 DB 中的值,则 DropDownList 将显示已选择的已租用、可用的和列表中的另一个已租用。我需要知道的是如何加载一个 DropDownList 来选择先前存储在数据库中的 PropertyStatus 枚举值而没有任何重复?

控制器:

        public ActionResult Edit(int id)
    {
        Property property = db.Properties.Find(id);

        ViewBag.PropertyStatus = SetViewBagPropertyStatus();
        return View(property);
    }

    private IEnumerable<SelectListItem> SetViewBagPropertyStatus()
    {
        IEnumerable<ePropStatus> values = 
            Enum.GetValues(typeof(ePropStatus)).Cast<ePropStatus>();

            IEnumerable<SelectListItem> items =
             from value in values
             select new SelectListItem
             {
                 Text = value.ToString(),
                 Value = value.ToString()
             };
            return items; 
    }

型号:

    public enum ePropStatus
    {
        Available ,
        Rented
    }

    public partial class Property
    {
         public int PropertyId { get; set; }
         public string PropName { get; set; }
         public string Address { get; set; }
         public string City { get; set; }
         public string State { get; set; }
         public string ZipCode { get; set; }
         public int SqFeet { get; set; }
         public int Bedrooms { get; set; }
         public int Bathrooms { get; set; }
         public int Garage { get; set; }
         public string Notes { get; set; }
         public ePropStatus PropertyStatus { get; set; }
    }

编辑视图:

@Html.DropDownList("PropertyStatus", Model.PropertyStatus.ToString())

【问题讨论】:

  • 尝试在您的控制器中使用它:return items.Distinct();
  • 添加 Distinct 无效。我仍然在下拉列表中获得重复的选择文本值。

标签: c# asp.net-mvc razor html-select


【解决方案1】:

试试这个:

@Html.DropDownListFor(model => model.PropertyStatus, ViewBag.PropertyStatus)

编辑 ::: 或者试试这个

控制者:

public ActionResult Edit(int id)
{
    Property property = db.Properties.Find(id);

    ViewBag.PropertyStatusList = SetViewBagPropertyStatus(property.PropertyStatus);
    return View(property);
}

private IEnumerable<SelectListItem> SetViewBagPropertyStatus(string selectedValue = "")
{
    IEnumerable<ePropStatus> values = 
        Enum.GetValues(typeof(ePropStatus)).Cast<ePropStatus>();

        IEnumerable<SelectListItem> items =
         from value in values
         select new SelectListItem
         {
             Text = value.ToString(),
             Value = value.ToString(),
             Selected = (selectedValue == value.ToString())
         };
        return items; 
}

查看:

@Html.DropDownList("PropertyStatus", ViewBag.PropertyStatusList)

【讨论】:

  • 不幸的是,这不起作用我得到 Model.Property 没有适用的方法 DropDownListFor 但似乎有一个名为该名称的扩展方法。扩展方法不能动态调度。
  • 我已经尝试过使用@Html.DropDownList("PropertyStatus", ViewBag.PropertyStatusList) 但它仍然返回相同的异常。
  • 现在我们使用的是“DropDownList”而不是“DropDownListFor”,那么这个异常怎么会发生呢?
  • 我不知道为什么这行代码会返回相同的异常@Html.DropDownList("PropertyStatus", ViewBag.PropertyStatusList)。
  • 我尝试了上面发布的其他示例,但我仍然收到此异常:编译器错误消息:CS1973:'System.Web.Mvc.HtmlHelper' has no applicable method名为“DropDownListFor”,但似乎具有该名称的扩展方法。扩展方法不能动态调度。考虑强制转换动态参数或调用扩展方法而不使用扩展方法语法。
猜你喜欢
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
相关资源
最近更新 更多