具有复杂视图模型的视图

发布于 2024-10-21 02:42:20 字数 402 浏览 1 评论 0原文

我正在使用 ASP.NET MVC 3,并且有一个视图模型,如下所示:

public class RegistrationViewModel
{
    public IList<LicenseViewModel> Licenses { get; set; }
}  

public class LicenseViewModel
{
    public string LicensedState { get; set; }
    public string LicenseType { get; set; }
}

用户可以在多个状态下获得许可,并且 LicensedState 和 LicenseType 值都应显示为网格页脚上的下拉列表。如何使用 RegistrationViewModel 创建视图?

I am using ASP.NET MVC 3 and I have a view model as follows:

public class RegistrationViewModel
{
    public IList<LicenseViewModel> Licenses { get; set; }
}  

public class LicenseViewModel
{
    public string LicensedState { get; set; }
    public string LicenseType { get; set; }
}

A user can be licensed in multiple states and both the LicensedState and LicenseType values should be presented as dropdowns on the footer of a grid. How can I create a view with the RegistrationViewModel?

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

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

发布评论

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

评论(2

淡紫姑娘! 2024-10-28 02:42:20

模型

public class RegistrationViewModel
{
    public IList<LicenseViewModel> Licenses { get; set; }
}

public class LicenseViewModel
{
    public string LicensedState { get; set; }
    public string LicenseType { get; set; }

    public IEnumerable<LicenseState> LicenseStates { get; set; }
    public IEnumerable<LicenseType> LicenseTypes { get; set; }
}

视图

@model RegistrationViewModel

@foreach (var item in Model)
{
    @Html.DropDownListFor(model => model.LicensedState, new SelectList(item.LicenseStates, item.LicenseState))
    @Html.DropDownListFor(model => model.LicenseType, new SelectList(item.LicenseTypes, item.LicenseType))
}

The Model

public class RegistrationViewModel
{
    public IList<LicenseViewModel> Licenses { get; set; }
}

public class LicenseViewModel
{
    public string LicensedState { get; set; }
    public string LicenseType { get; set; }

    public IEnumerable<LicenseState> LicenseStates { get; set; }
    public IEnumerable<LicenseType> LicenseTypes { get; set; }
}

The View

@model RegistrationViewModel

@foreach (var item in Model)
{
    @Html.DropDownListFor(model => model.LicensedState, new SelectList(item.LicenseStates, item.LicenseState))
    @Html.DropDownListFor(model => model.LicenseType, new SelectList(item.LicenseTypes, item.LicenseType))
}
雨落星ぅ辰 2024-10-28 02:42:20

您可以拥有这样的视图模型:

public class LicenseViewModel
{
  public IEnumerable<SelectListItem> LicensedState { get; private set; }
  public IEnumerable<SelectListItem> LicenseType { get; private set; }

  public LicenseViewModel(string licensedState = null, string licenseType = null)
  {
    LicensedState = LicensedStatesProvider.All().Select(s=> new SelectListItem
      {Selected = licensedState!= null && s == licensedState, Text = s, Value = s} );
    LicenseType = LicenseTypesProvider.All().Select(t => new SelectListItem
      { Selected = licenseType != null && t == licenseType, Text = t, Value = t });
  }
}

LicensedStatesProviderLicenseTypesProvider 是获取所有 LicensedStates 和 LicenseType 的简单方法,如何获取它们取决于您。

考虑到,你会看到这样的东西:

@foreach (var license in Model.Licenses)
{
  //other stuff...  
  @Html.DropDownList("LicensedState", license.LicensedState)
  @Html.DropDownList("LicenseType", license.LicenseType)
}

You can have your view model like this:

public class LicenseViewModel
{
  public IEnumerable<SelectListItem> LicensedState { get; private set; }
  public IEnumerable<SelectListItem> LicenseType { get; private set; }

  public LicenseViewModel(string licensedState = null, string licenseType = null)
  {
    LicensedState = LicensedStatesProvider.All().Select(s=> new SelectListItem
      {Selected = licensedState!= null && s == licensedState, Text = s, Value = s} );
    LicenseType = LicenseTypesProvider.All().Select(t => new SelectListItem
      { Selected = licenseType != null && t == licenseType, Text = t, Value = t });
  }
}

LicensedStatesProvider and LicenseTypesProvider are simply way of getting all LicensedStates and LicenseTypes, it's up to you how to get them.

And in view, you'd have something like this:

@foreach (var license in Model.Licenses)
{
  //other stuff...  
  @Html.DropDownList("LicensedState", license.LicensedState)
  @Html.DropDownList("LicenseType", license.LicenseType)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文