编辑视图-下拉列表选择选项-分配当前值 [英] Edit View - Droplist select option - dislaying the current value

查看:85
本文介绍了编辑视图-下拉列表选择选项-分配当前值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a edit view - my question is on the code below how would I have the selected value show the current value in the database? Example let's say the value of PaymentStatus is Weekly. I want the list to show Weekly, not Annually. I guess somehow dynamically change he selected option.

谢谢,EB

工资单:

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);
       }

推荐答案

在讨论了聊天之后,这些是为了实现所需内容而需要采取的步骤.

After the chat disscussion, These are the steps to take in order to acheive what you want.

控制器:

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>

这篇关于编辑视图-下拉列表选择选项-分配当前值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆