【问题标题】:mvc dropdownlist selected item issuemvc 下拉列表选择的项目问题
【发布时间】:2014-08-26 17:00:13
【问题描述】:

大家晚上好,我有一个关于下拉选择项目的问题。我的数据库中有一个数据,我正在选择并在我的网格中显示它们。但是,当我希望他们编辑下拉字段时,我不会选择。我试着这样做,这是我的代码,但它没有用。我在这里遗漏了什么或者我需要更改此代码吗?

public ActionResult OrderEdit(int id)
    {
        Repository<Order> _ro = new Repository<Order>();
        Order _order = _ro.Find(o => o.id == id);

        int _selectedId=_order.customerProductTypeId;

        //IEnumerable<CustomerProductType> _departmentList = _rd.All();
        //var _department = _departmentList.Where(d => d.id == id).FirstOrDefault();

        Repository<Customer> _rc = new Repository<Customer>();
        IEnumerable<SelectListItem> _customerSelectListItem = _rc.All().AsEnumerable().Select(s =>
            new SelectListItem
            {
                Text = s.name,
                Value = s.id.ToString(),
                Selected=this.MySelected(s.id,1)
            });


        ViewData["ddCust"] = _customerSelectListItem;

        Repository<CustomerProductType> _rcp = new Repository<CustomerProductType>();
        IEnumerable<SelectListItem> _customerProductTypeSelectListItem = _rcp.All().AsEnumerable().Select(s =>
            new SelectListItem
            {
                Text = s.name,
                Value = s.id.ToString(), 
                Selected=this.MySelected(s.id,_selectedId)
            });



        ViewData["ddDepartment"] = _customerProductTypeSelectListItem;

        //IEnumerable<MyCustomerProductType> _customerProductType = _rd.All().Select(s=> new MyCustomerProductType {id = s.id, customerName=s.customer.name,productTypeName = s.productType.name,name=s.name});
        //var _department = _customerProductType.Where(a => a.id == id).FirstOrDefault();

        return View(_order);
    }

    private bool MySelected(int _id, int _selected)
    {
        bool _b = false;

        if (_id.Equals(_selected))
            _b=true;

        return _b;
    }

而我的看法是这样的

@using (Html.BeginForm("EditOrder","Order",FormMethod.Post)) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Order</legend>

    @Html.HiddenFor(model => model.id)

    <div class="editor-label">
        @Html.LabelFor(model => model.code)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.code)
        @Html.ValidationMessageFor(model => model.code)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.customerProductType.customerId)
    </div>
    <div class="editor-field">
        @*@Html.EditorFor(model => model.customerId)*@
        @Html.DropDownList("ddCust",(IEnumerable<SelectListItem>)ViewData["ddCust"],"secim yapınız", new { @class = "span6 chosen" })
        @Html.ValidationMessageFor(model => model.customerProductType.customerId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.customerProductTypeId)
    </div>
    <div class="editor-field">
        @*@Html.EditorFor(model => model.customerProductTypeId)*@
        @*@Html.DropDownList("ddDepartment",(IEnumerable<SelectListItem>)ViewData["ddDepartment"],"secim yapınız", new { @class = "span6 chosen" })*@
        @Html.ValidationMessageFor(model => model.customerProductTypeId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.price)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.price)
        @Html.ValidationMessageFor(model => model.price)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.orderDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.orderDate)
        @Html.ValidationMessageFor(model => model.orderDate)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.state)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.state)
        @Html.ValidationMessageFor(model => model.state)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

谢谢。

【问题讨论】:

    标签: asp.net asp.net-mvc-4 razor selecteditem html.dropdownlistfor


    【解决方案1】:

    这样做

    @Html.DropDownListFor(m => m.customerProductTypeId,(IEnumerable<SelectListItem>)ViewData["ddCust"],"secim yapınız", new { @class = "span6 chosen" })
    

    customerProductTypeId 是您获取和设置下拉菜单值的地方

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      相关资源
      最近更新 更多