0

Good Day everyone. I have next question.

I have Edit form with few DropDownList controls on it. I need to bind selected value in DropDownListFor based upon data which I have in my db.

What I have is next:

Model:

Supplier

public class Supplier{
    public int SupplierID { get; set; }       
    public string CompanyName { get; set; }
}

Delviery

public class Delivery{
    public int DeliveryID { get; set; }
    public string DeliveryCode { get; set; }
    public int SupplierID { get; set; }
    public virtual Supplier Supplier { get; set; }        
}

ViewModel for Delivery Edit Screen:

public class DeliveryEditViewModel{
    public int DeliveryID { get; set; }        
    public string SelectedCompanyName { get; set; }
    public IEnumerable<SelectListItem> Suppliers { get; set; }
}

View:

<dd>
    @Html.DropDownListFor(x => x.SelectedCompanyName, new SelectList(Model.Suppliers, "Value", "Text"), htmlAttributes: new { @class = "form-control" })
</dd>

Controller:

 // GET: Deliveries/Edit
public ActionResult Edit(int? id){   
    db.Configuration.ProxyCreationEnabled = false;
        DeliveryEditViewModel model = db.Deliveries.Include(s => s.Supplier).Include(a => a.Auction).Include(q => q.Quality).Include(t => t.BulbType)
                .Where(d => d.DeliveryID == id)
                .Select(x => new DeliveryEditViewModel{
                    DeliveryID = x.DeliveryID,
                    SelectedCompanyName = x.Supplier.CompanyName
                })
                .Single();               
    model.Suppliers = db.Suppliers.Where(s => s.IsActive == true).Select(x => new SelectListItem{
        Value = x.SupplierID.ToString(),
        Text = x.CompanyName
    });
    return View(model);
}

Problem is that I can't setup selected value in DropDownListFor based upon data which I already have inside my database.Instead I have DropDown control with first element active always. I already tried solutoin from Html.DropdownListFor selected value not being set but still I have first value selected instead of real one.

Hours of researching and already bold hair makes me to stop and ask for a help. Please help me to figure out how it could be possible.

Thank you so much.

Community
  • 1
  • 1
  • `Suppliers` is already `IEnumerable` Why in the world are you creating another identical one using `new SelectList(Model.Suppliers, "Value", "Text")` in the view? You need to set the value of `SelectedCompanyName` to match one of the options (i.e one of the values of `SupplierID` in your `Suppliers` table (but your code suggest the property should be `int` not `string`) –  Jul 19 '16 at 10:20
  • @StephenMuecke, thank you so much. I do not know how can I miss this. Now it works perfectly. You save me ) – Женька Сидоров Jul 19 '16 at 10:36

2 Answers2

0

Suppliers is already IEnumerable<SelectListItem> Why in the world are you creating another identical one using new SelectList(Model.Suppliers, "Value", "Text") in the view? You need to set the value of SelectedCompanyName to match one of the options (i.e one of the values of SupplierID in your Suppliers table (but your code suggest the property should be int not string) by @StephenMuecke

-1

As what i will advice you not create SelectListItems at controller level. What you can do is :

public class DeliveryEditViewModel
{
    public int DeliveryID { get; set; }        
    public string SelectedCompanyName { get; set; }
    public IEnumerable<Supplier> suppliers { get; set; }
}

Controller :

 // GET: Deliveries/Edit
public ActionResult Edit(int? id){   
    db.Configuration.ProxyCreationEnabled = false;
        DeliveryEditViewModel model = db.Deliveries.Include(s => s.Supplier).Include(a => a.Auction).Include(q => q.Quality).Include(t => t.BulbType)
                .Where(d => d.DeliveryID == id)
                .Select(x => new DeliveryEditViewModel{
                    DeliveryID = x.DeliveryID,
                    SelectedCompanyName = x.Supplier.CompanyName
                })
                .Single();               
    Model.suppliers = db.Suppliers.Where(s => s.IsActive == true);
    return View(model);
}

View :

  @Html.DropDownListFor(model => model.SelectedCompanyName, ((IEnumerable<Supplier>)Model.supplierss).Select(x => new SelectListItem()
           {
               Text = x.CompanyName.ToString(),
               Value = x.SupplierID.ToString(),
               Selected = (Model != null) && (x.CompanyName == Model.SelectedCompanyName)
           }), "-- Select Supplier --")

This is a very usefull trick, you can change Text or Value or Selected Value or Default Selected options without changing at controller and building it again to reflect.

Salman Zahid
  • 328
  • 1
  • 7