【问题标题】:Edit View - Droplist select option - dislaying the current value编辑视图 - 下拉列表选择选项 - 显示当前值
【发布时间】:2019-08-01 15:18:14
【问题描述】:

我创建了一个编辑视图 - 我的问题是关于下面的代码,我如何让选定的值显示数据库中的当前值?例如,假设 PaymentStatus 的值为 Weekly。我希望列表显示每周,而不是每年。我猜想以某种方式动态改变他选择的选项。

谢谢, 乙

控制器:

List<SelectListItem> Payment = new List<SelectListItem>()
{
    new SelectListItem { Text = "Annually", Value = "Annually" },
    new SelectListItem { Text = "Monthly", Value = "Monthly" },
    new SelectListItem { Text = "Weekly", Value = "Weekly" },
    new SelectListItem { Text = "Daily", Value = "Daily" }
};

ViewBag.PaymentStructure = Payment;

查看:

<div class="form-group">

    @Html.LabelFor(model => model.PaymentStructure, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.PaymentStructure, (IEnumerable<SelectListItem>)ViewBag.PaymentStructure, null, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.PaymentStructure, "", new { @class = "text-danger" })
    </div>
</div>

完整的控制器:

public ActionResult EditOrder(int id)
      {
        List<SelectListItem> Payment = new List<SelectListItem>()
          {
            new SelectListItem { Text = "Annually", Value = "Annually" },
            new SelectListItem { Text = "Monthly", Value = "Monthly" },
            new SelectListItem { Text = "Weekly", Value = "Weekly" },
            new SelectListItem { Text = "Daily", Value = "Daily" },
         };
        ViewBag.PaymentStructure = Payment;
        var order = db.Orders.Find(id);
        if (order == null)
           {
           return HttpNotFound();
           }
           return View(order);
       }

【问题讨论】:

  • 试试这个@Html.DropDownListFor(model =&gt; model.Status, new SelectList(your list), "- Please Select -", new { })
  • 我更新了我原来的帖子 - 它似乎仍然不起作用。有任何想法吗?谢谢!
  • 在将模型放到视图之前,您可以向控制器展示您设置模型的位置吗?
  • OK - 看看是否有帮助。我添加了完整的控制器代码。
  • @EbertB 你找到解决方案了吗?可以分享一下吗?

标签: asp.net asp.net-mvc


【解决方案1】:

在聊天讨论之后,这些是为了实现你想要的而采取的步骤。

控制者:

public ActionResult EditOrder(int id)
{
    List<SelectListItem> Payment = new List<SelectListItem>()
    {
        new SelectListItem { Text = "Annually", Value = "Annually" },
        new SelectListItem { Text = "Monthly", Value = "Monthly" },
        new SelectListItem { Text = "Weekly", Value = "Weekly" },
        new SelectListItem { Text = "Daily", Value = "Daily" },
    };
    ViewBag.PaymentStructure = Payment;

    var order = db.Orders.Find(id);
    //NOTE for the dropdown to populate, order.PaymentStructure must have any of this values ("Annually", "Monthly","Weekly","Daily"), debug at this point to confirm the value of order.PaymentStructure
    if (order == null)
    {
        return HttpNotFound();
    }
    return View(order);
}

观点: 没有变化

<div class="form-group">

    @Html.LabelFor(model => model.PaymentStructure, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.PaymentStructure, (IEnumerable<SelectListItem>)ViewBag.PaymentStructure, null, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.PaymentStructure, "", new { @class = "text-danger" })
    </div>
</div>

【讨论】:

    【解决方案2】:

    我认为最简单的方法是使用“DropDownListFor”,如下所示

    @Html.DropDownListFor(model => model.Status, new SelectList(itemslist), "[Select an Item]", new { @class = "form-control" })
    

    如果您需要使用引导程序或任何其他 Html 属性,请使用 @class = "form-control"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-15
      • 1970-01-01
      • 2020-07-31
      • 2022-01-08
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      相关资源
      最近更新 更多