mvc dropdownlist选择项目问题

时间:2014-07-05 19:20:34

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

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

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>
}
谢谢。

1 个答案:

答案 0 :(得分:2)

喜欢

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

customerProductTypeId是您将获得的位置并设置下拉列表的值