使用不带魔术字符串的 SelectList

发布于 2024-10-27 19:09:48 字数 1135 浏览 6 评论 0原文

有没有一种简单的方法可以在创建 SelectList 时删除魔术字符串的使用,如本例所示:

@Html.DropDownListFor( model => model.FooValue, new SelectList( Model.FooCollection,  "FooId", "FooText", Model.FooValue) )

魔术字符串是 "FooId""FooText"

其余的该示例定义如下:

//Foo Class
public class Foo {

  public int FooId { get; set; }
  public string FooText { get; set; }

}    

// Repository
public class MsSqlFooRepository : IFooRepository {

  public IEnumerable<Foo> GetFooCollection( ) {

    // Some database query

  }

}

//View model
public class FooListViewModel {

  public string FooValue { get; set; }
  public IEnumerable<Foo> FooCollection { get; set; }

}

//Controller
public class FooListController : Controller {

  private readonly IFooRepository _fooRepository;

  public FooListController() {

    _fooRepository = new FooRepository();

  }

  public ActionResult FooList() {

    FooListViewModel fooListViewModel = new FooListViewModel();

    FooListViewModel.FooCollection = _fooRepository.GetFooCollection;

    return View( FooListViewModel);

  }

}

Is there an easy way to remove the use of Magic Strings when creating a SelectList, like in this example:

@Html.DropDownListFor( model => model.FooValue, new SelectList( Model.FooCollection,  "FooId", "FooText", Model.FooValue) )

The magic strings being "FooId" and "FooText"

The rest of the example is defined as follows:

//Foo Class
public class Foo {

  public int FooId { get; set; }
  public string FooText { get; set; }

}    

// Repository
public class MsSqlFooRepository : IFooRepository {

  public IEnumerable<Foo> GetFooCollection( ) {

    // Some database query

  }

}

//View model
public class FooListViewModel {

  public string FooValue { get; set; }
  public IEnumerable<Foo> FooCollection { get; set; }

}

//Controller
public class FooListController : Controller {

  private readonly IFooRepository _fooRepository;

  public FooListController() {

    _fooRepository = new FooRepository();

  }

  public ActionResult FooList() {

    FooListViewModel fooListViewModel = new FooListViewModel();

    FooListViewModel.FooCollection = _fooRepository.GetFooCollection;

    return View( FooListViewModel);

  }

}

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

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

发布评论

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

评论(3

苏璃陌 2024-11-03 19:09:48

使用扩展方法和 lambda 表达式的强大功能,您可以做到这一点:

@Html.DropDownListFor(model => model.FooValue, Model.FooCollection.ToSelectList(x => x.FooText, x => x.FooId))

扩展方法如下:

public static class SelectListHelper
{
    public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, string> text, Func<T, string> value)
    {
        var items = enumerable.Select(f => new SelectListItem()
        {
            Text = text(f),
            Value = value(f)
        }).ToList();
        items.Insert(0, new SelectListItem()
        {
            Text = "Choose value",
            Value = string.Empty
        });
        return items;
    }
}

Using an extension method and the power of lambda expressions, you can do this:

@Html.DropDownListFor(model => model.FooValue, Model.FooCollection.ToSelectList(x => x.FooText, x => x.FooId))

The extension method is as follows:

public static class SelectListHelper
{
    public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, string> text, Func<T, string> value)
    {
        var items = enumerable.Select(f => new SelectListItem()
        {
            Text = text(f),
            Value = value(f)
        }).ToList();
        items.Insert(0, new SelectListItem()
        {
            Text = "Choose value",
            Value = string.Empty
        });
        return items;
    }
}
趁年轻赶紧闹 2024-11-03 19:09:48

我使用视图模型,因此我的 FooValues 下拉列表具有以下属性:

public SelectList FooValues { get; set; }
public string FooValue { get; set; }

然后在构建视图模型的代码中我这样做:

viewModel.FooValues = new SelectList(FooCollection, "FooId", "FooText", viewModel.FooValue);

然后在我看来:

@Html.DropDownListFor(m => m.FooValue, Model.FooValues)

我希望这会有所帮助。

I use View Models so I have the following properties for my FooValues dropdown:

public SelectList FooValues { get; set; }
public string FooValue { get; set; }

and then in my code for building my view model I do:

viewModel.FooValues = new SelectList(FooCollection, "FooId", "FooText", viewModel.FooValue);

Then in my view:

@Html.DropDownListFor(m => m.FooValue, Model.FooValues)

I hope this helps.

赴月观长安 2024-11-03 19:09:48

在 C# 6 中,您可以利用 nameof 并轻松摆脱这些神奇的字符串。

... = new SelectList(context.Set<User>(), nameof(User.UserId), nameof(User.UserName));

In C# 6 you can take advantage of nameof and easily get rid of these magic strings.

... = new SelectList(context.Set<User>(), nameof(User.UserId), nameof(User.UserName));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文