|
-
Aug 8th, 2011, 01:27 PM
#1
[RESOLVED] MVC 3 DropDownListFor problem
Hello.
I'm using VS2010, MVC 3, Razor and Entity Framework 4.
I want to have a drop down list with list of all countries from the Countries table. And in my model, I have just a single country id (Guid).
So, in my Model, I have:
Code:
public Guid? CountryID
{ get; set; }
In my Controller, I have:
Code:
ViewBag.Countries = from e in new MyEntities().Countries select e;
And in my View, I have:
Code:
@Html.DropDownListFor(m => m.CountryID, new SelectList(ViewBag.Countries, "CountryID", "Name"), "")
If I choose something or not, i get the "Value cannot be null", on the View line of the code (@Html.DropDown...). What am I doing wrong?
Also, cause I'm totally new to MVC and Entity Framework, is there a better way to achieve this? I'm Googling this for few hours now with no results...
EDIT: I have just seen I've posted in the wrong forum section, so please, redirect this. Sorry.
-
Aug 9th, 2011, 12:50 AM
#2
Re: MVC 3 DropDownListFor problem
 Originally Posted by gavio
EDIT: I have just seen I've posted in the wrong forum section, so please, redirect this. Sorry.
No problem 
Moved to the MVC Forums.
Gary
-
Aug 9th, 2011, 03:06 AM
#3
Re: MVC 3 DropDownListFor problem
Ok. Got it working with a little bit of a different approach.
Model:
Code:
public UserRegistrationModel()
{
this.InitializeCountries();
}
private void InitializeCountries()
{
FarmerEntities fe = new FarmerEntities();
var query = from c in new FarmerEntities().Countries select new { ID = c.ID_Country, Name = c.ISO_Code };
var countries = query.ToSelectList(c => c.ID.ToString(), c => c.Name);
this.Countries = countries;
}
public Guid? { get; set; }
public IEnumerable<SelectListItem> Countries
{ get; set; }
Controller:
Code:
UserRegistrationModel model = new UserRegistrationModel();
return View(model);
View:
Code:
@Html.DropDownListFor(m => m.Country, Model.Countries, "")
Also, you need to implement this extension:
Code:
public static class EnumerableExtensions
{
public static IEnumerable<SelectListItem> ToSelectList<TItem, TValue>(this IEnumerable<TItem> items, Func<TItem, TValue> valueSelector, Func<TItem, string> nameSelector)
{
return items.ToSelectList(valueSelector, nameSelector, x => false);
}
public static IEnumerable<SelectListItem> ToSelectList<TItem, TValue>(this IEnumerable<TItem> items, Func<TItem, TValue> valueSelector, Func<TItem, string> nameSelector, IEnumerable<TValue> selectedItems)
{
return items.ToSelectList(valueSelector, nameSelector, x => selectedItems != null && selectedItems.Contains(valueSelector(x)));
}
public static IEnumerable<SelectListItem> ToSelectList<TItem, TValue>(this IEnumerable<TItem> items, Func<TItem, TValue> valueSelector, Func<TItem, string> nameSelector, Func<TItem, bool> selectedValueSelector)
{
foreach (var item in items)
{
var value = valueSelector(item);
yield return new SelectListItem
{
Text = nameSelector(item),
Value = value.ToString(),
Selected = selectedValueSelector(item)
};
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|