2

In the Model, I have:

public Guid? Country
{ get; set; }

In the View, I have:

@Html.DropDownListFor(m => m.Country, (SelectList)ViewBag.Countries)

When submitting, error occures, cause it wants to save the string to the Guid. How to save the value rather then the text of the drop down list?

4
  • As a work-around you could add a CountryString property that wraps the GUID and set up your DropDownListFor to use that, but there must be a better way. Commented Aug 8, 2011 at 17:21
  • Ok. I've changed my code a little and this is what I'm using in the view: @Html.DropDownListFor(m => m.Country, new SelectList(ViewBag.Countries, "ID_Country", "ISO_Code"), ""); basically, I've just moved the SelectList() constructor call from controller to the view. And this is how I get countries to the ViewBag from the Controller (via Entity Framework): ViewBag.Countries = from e in new FarmerEntities().Countries select e;. Now I get the "Value cannot be null" exception (if I choose something or not). Please help! Commented Aug 8, 2011 at 18:14
  • 1
    From the looks of it, you are using ISO country codes (which are not GUIDs). Is there a reason your data type for the country is a Guid instead of a string? Commented Aug 8, 2011 at 20:32
  • No. "ID_Country" is for DropDownLists' "hidden" value list and "ISO_Code" is for DropDownLists' "front"/visible value list. Everything works fine until submit. Commented Aug 9, 2011 at 7:14

1 Answer 1

2

Ok. Got it working with a little bit of a different approach.

Model:

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:

UserRegistrationModel model = new UserRegistrationModel();

return View(model);

View:

@Html.DropDownListFor(m => m.Country, Model.Countries, "")

Also, you need to implement this extension

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)
            };
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.