locked
Dropdownlist doesn't select the database value in edit mode RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    Below is my dropdownlist. I have an issue.

    In my database there is a value in column which is "Mr." I am binding my dropdownlist with some values like Mr., Mrs. Dr. etc.

    But when I get into  in edit mode the drodownlist does not select this value, insted it always shows the -Select- which is default.

     <div class="form-group">
                                @Html.LabelFor(model => model.Salutation, htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-6">
                                    @Html.DropDownListFor(model => model.Salutation, new SelectList(ViewBag.Salutation, "Value", "Text"), "Select", new { @id = "ddlSalutation", @class = "form-control" })
                                </div>
                            </div>

    Below is my controller cod to bind the dropdown list

    #region Bind Salutation
                ViewBag.Salutation = (from p in db.Salutation_Master.AsEnumerable().Where(m => m.status == true)
                                      select new SelectListItem
                                      {
                                          Text = p.vctitle,
                                          Value = p.vctitle,
                                      }).ToList();
                #endregion

    An another confusion that by default what the dropdownlist save in database it's text or value? How can we manage the I need to save value or text?

    Please suggest.

    Friday, August 2, 2019 5:07 AM

Answers

  • User-1038772411 posted

    Hello, demoninside9

    View :

     @Html.DropDownList("ProductParentCategoryId", null, htmlAttributes: new { @class = "form-control" })
    <time itemprop="dateCreated" datetime="2017-02-21T10:43:11"></time>
    Controller :
    [HttpGet]
        public ActionResult Edit(int id)
        {
            var selectedId = _productCategoryService.GetOneProductCategory(id);
    
            ViewBag.ProductParentCategoryId = new SelectList(_productCategoryService.GetAllProductCategory(), "ProductCategoryId", "ProductCategoryTitle", (int)selectedId.ProductParentCategoryId);
            ViewBag.GroupFiltersId = new SelectList(_groupFiltersService.GetAllGroupFilter().Where(a => a.GroupFilterParentId == null), "GroupFilterId", "GroupFilterTitle");
            return View(_productCategoryService.GetOneProductCategory(id));
        }

    Reference Link :

    https://stackoverflow.com/questions/42352508/asp-net-mvc-model-binder-does-not-work-for-drop-down-list-in-edit-mode

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2019 9:53 AM

All replies

  • User-1038772411 posted

    Hello, demoninside9

    View :

     @Html.DropDownList("ProductParentCategoryId", null, htmlAttributes: new { @class = "form-control" })
    <time itemprop="dateCreated" datetime="2017-02-21T10:43:11"></time>
    Controller :
    [HttpGet]
        public ActionResult Edit(int id)
        {
            var selectedId = _productCategoryService.GetOneProductCategory(id);
    
            ViewBag.ProductParentCategoryId = new SelectList(_productCategoryService.GetAllProductCategory(), "ProductCategoryId", "ProductCategoryTitle", (int)selectedId.ProductParentCategoryId);
            ViewBag.GroupFiltersId = new SelectList(_groupFiltersService.GetAllGroupFilter().Where(a => a.GroupFilterParentId == null), "GroupFilterId", "GroupFilterTitle");
            return View(_productCategoryService.GetOneProductCategory(id));
        }

    Reference Link :

    https://stackoverflow.com/questions/42352508/asp-net-mvc-model-binder-does-not-work-for-drop-down-list-in-edit-mode

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 2, 2019 9:53 AM
  • User1520731567 posted

    Hi demoninside9,

    An another confusion that by default what the dropdownlist save in database it's text or value? How can we manage the I need to save value or text?

    @Html.DropDownListFor(model => model.Salutation, new SelectList(ViewBag.Salutation, "Value", "Text"), "Select", new { @id = "ddlSalutation", @class = "form-control" })

    ViewBag.Salutation = (from p in db.Salutation_Master.AsEnumerable().Where(m => m.status == true)
    select new SelectListItem
    {
    Text = p.vctitle,
    Value = p.vctitle,
    }).ToList();

    text or value,it is depend on yourself.

    But the type of data which stored in database must be consistent with model.Salutation.

    In other words,if your vctitle is string type,Salutation must be string type.

    According to your code,I didn't find anything wrong.

    So I guess maybe your type is not match between Salutation and value of new SelectList().

    If you have any questions,please post more details.

    Best Regards.

    Yuki Tao

    Monday, August 5, 2019 9:07 AM