Click here to Skip to main content
15,888,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to MVC and am wondering if someone can please help?

I have the following which populates the DropDownList for each row in the model yet does not set the selected value (tblcusts.typeid) in the DropDownList when the page is populated. Any assistance would be greatly appreciated.

What I have tried:

Models
public class tblcusts
{
   [Key]
   public int custid { get; set; }
   public int typeid { get; set; }
}

public class tblcusttypes
{
  [Key]
  public int typeid { get; set; }
  public string name { get; set; }
  public bool active { get; set; }
}

View
@Model IList<MvcApplication1.Models.tblcusts>
@for (var i = 0; i < Model.Count; ++i)
    {
       @Html.DropDownList("typeid", (IEnumerable<SelectListItem>)ViewBag.customertype)
       <br />
    }

Controller
var result = from m1 in db.custs select m1;

var query = from m2 in db.custtypes where m2.active == true orderby m2.name select m2.name;
var CustomerTypeList = new List<string>();
CustomerTypeList.AddRange(query.Distinct());
ViewBag.customertype = new SelectList(CustomerTypeList);

return View(result.ToList());
Posted
Updated 26-May-17 9:10am

1 solution

Your not getting the selected value because your not setting any selected value. Also, your view is confusing, your looping over the drop down list in your view, you should be doing that in your model or controller (preferably model)....but not i guess its to show 1 type drop down per customer? Anywho...

To set the selected value on SelectList's you need to do something like this.

C#
List<string> dropdown_content = new List<string>();
var query = from m2 in db.custtypes where m2.active == true orderby m2.name select m2.name;
SelectList list = new SelectList(query);

// This is where you find the selected item and if it isn't null, set Selected == true
var selectedItem = list.FirstOrDefault(m=>m.Value == id);

if(selectedItem != null)
{
   selectedItem.Selected = true;
}    

ViewBag.customertype = list;
 
Share this answer
 
Comments
Timothy Vandeweerd 26-May-17 16:00pm    
Thank you David, I changed:

from: var selectedItem = list.FirstOrDefault(m => m.Value == id);
to: var selectedItem = list.FirstOrDefault(m1 => m1.Value == typeid);

The error highlights the assignment "== typeid);" and shows: "The name 'typeid' does not exist in the current context"

Am I understanding things properly? My view will list rows from tblcusts (m1) and each row will have a DropDownList showing all the rows from tblcusttypes (m2) with the SelectedValue set based on the rows value of tblcusts.typeid which will can be different for each row of tblcusts.
David_Wimbley 27-May-17 2:46am    
Is typeid a variable? In your code sample its a string. Without knowing more about your code, it looks like you need to loop over tblcusts which is what your view has as its Model, that for loop in your view. Loop through that in your Model class, decide if the value is there and set Selected == true.

SelectList has the following properties on it. Text, Value, Selected. Value being what is saved, Text what is displayed and Selected to indicate if anything has been selected.

You'll need to do a comparison to say "Does my value in the select list match my value from the database, if so -> .Selected is set to true".
Timothy Vandeweerd 29-May-17 9:49am    
David,

Thank you for your response. typeid is an int column in both tables. It is a Primary Key in tblcusttypes and a Foreign Key in tblcusts. My view will display multiple rows when the page is loaded and for each row in tblcusts I would like to have a DropDownList and the SeclectedValue to be set to the Foreign Key value. Thank you for your time, I have been searching and searching and cannot seem to find an example of this online.

This is my code:

Models

public class tblcusts
{
[Key]
public int custid { get; set; }
public int typeid { get; set; }
}

public class tblcusttypes
{
[Key]
public int typeid { get; set; }
public string name { get; set; }
public bool active { get; set; }
}

View

@model IList<mvcapplication1.models.tblcusts>
@for (var i = 0; i < Model.Count; ++i)
{
@Html.DropDownList("typeid", (IEnumerable<selectlistitem>)ViewBag.customertype)
}

Controller

var result = from m1 in db.custs select m1;

var query = from m2 in db.custtypes where m2.active == true orderby m2.name select m2.name;
var CustomerType4= new List<string>();
CustomerType4.AddRange(query.Distinct());
ViewBag.customertype = new SelectList(CustomerType4);
David_Wimbley 29-May-17 15:16pm    
Hey Timothy,

Looks like in your controller you need to loop over ViewBag.customertype to set what is selected. I'd make the following changes.

Instead of


ViewBag.customertype = new SelectList(CustomerType4);


I would change it to


var selectList = new SelectList(CustomerTyp4);
foreach(var item in selectList)
{
if(item.Value == typeid)
{
item.Selected = true;
}
}
Timothy Vandeweerd 29-May-17 15:32pm    
David, thank you but now the line below produces an error:

if (item.Value == typeid)

Error: The name 'typeid' does not exist in the current context

I really appreciate your time on this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900