Reputation: 1
In MVC.
I am getting this error message when trying to display a dropDownList in Razor.
DataBinding: 'System.Int32' does not contain a property with the name 'BranchId'.
In the watch window I am able to see 1 record is being returned from the database containing a BranchId
and BranchName
I have no reference to the SelectList
in the Model class.
In the Controller class
var list = new SelectList(db.Branchs.Where(branches => !db.CompanyBranches
.Any(d => d.BranchId == branches.BranchId))
.Select(branches => branches.BranchId).ToList(),
"BranchId", "BranchNumber");
ViewBag.Branch = new SelectList(list);
In the Create.cshtml
@Html.DropDownList("Branch", new SelectList(ViewBag.Branch, "BranchID", "BranchName"), htmlAttributes: new { @class = "form-control" })
Thanks Stephen
I changed the lambda expression in my CompanyBranchesController as you suggested it is now var list = db.Branchs.Where(branches => !db.CompanyBranches .Any(d => d.BranchId == branches.BranchId)) .Select(b => new { BranchId = b.BranchId, BranchName = b.BranchName }).ToList();
The Create.cshtml for the dropdownlist is @Html.DropDownList("Branch", ViewBag.Branch as IEnumerable, htmlAttributes: new { @class = "form-control" })
The result in the dropdownlist is {BranchId = 5, BranchNumber = Br0003}
I have been playing with it including adding to the CompanyBranchController
List items = new List();
foreach (var i in list)
{
SelectListItem s = new SelectListItem();
s.Text = i.BranchName.ToString();
s.Value = i.BranchId.ToString();
items.Add(s);
}
As well as trying different razor expressions but with no success.
Any Idea where I am going wrong?
Upvotes: 0
Views: 1261
Reputation: 1
Your code was correct except that in the CompanyBranchContoller I added
List<SelectListItem> items = new List<SelectListItem>();
foreach (var i in list)
{
SelectListItem s = new SelectListItem();
s.Text = i.BranchName.ToString();
s.Value = i.BranchId.ToString();
items.Add(s);
}
ViewBag.Branch = items;
Then in the Create.cshtml class I changed the reference to the dropdown list to what you suggested:
@Html.DropDownList("Branch", ViewBag.Branch as IEnumerable<SelectListItem>, htmlAttributes: new { @class = "form-control" })
Thanks for all of your help Stephen
Paul
Upvotes: 0
Reputation: 218732
Look at this part of your code (The SelectList constructor)
Select(branches => branches.BranchId).ToList()
You are selecting the branchId's. So basically you are passing a list of integers to the SelectList
constructor. But you specify that BranchId
is the data value field. But the integer does not have such a property.
So you should change your Select to an annonymous type which has this properties
Select(b => new { BranchId = b.BranchId, BranchNumber = b.BranchId).ToList()
Also you do not need to create another SelectList again when you assign to the ViewBag. So this is good enough
ViewBag.Branch = list;
and in your view,
@Html.DropDownList("Branch", ViewBag.Branch as IEnumerable<SelectListItem>,
htmlAttributes: new { @class = "form-control" })
Or even easier, you may simply convert your items to a list of SelectListItem
var list= db.Branchs.Where( // Put your where clause here)
.Select(branches => new SelectListItem { Value = branches.BranchId.ToString(),
Text = branches.BranchId.ToString()}).ToList();
ViewBag.Branch = list;
Upvotes: 1