ASP .NET MVC 3 DropDownListFor问题

发布于 2024-11-28 15:14:36 字数 242 浏览 0 评论 0原文

在模型中,我有:

public Guid? Country
{ get; set; }

在视图中,我有:

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

提交时,发生错误,因为它想将字符串保存到 Guid。如何保存下拉列表的值而不是文本?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心奴独伤 2024-12-05 15:14:36

好的。用一点不同的方法让它工作。

模型:

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; }

控制器:

UserRegistrationModel model = new UserRegistrationModel();

return View(model);

视图:

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

另外,你需要实现这个扩展

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)
            };
        }
    }
}

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)
            };
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文