c# - 我应该如何构建我的 View 模型 - ASP.NET MVC

标签 c# asp.net-mvc asp.net-mvc-3 model-view-controller viewmodel

我想使用 ASP.NET MVC 3.0 创建一个调查。有些问题会有单选按钮和一些复选框。我想知道我的 View 端和 ViewModel 的正确语法。 我想将我的答案选项存储在一个集合中。 IEnumerable 会是一个很好用的集合吗?这是我的一些 ViewModel 代码。

    [DisplayName("Country")]
    [Required(ErrorMessage = "Country is required.")]
    public string Country { get; set; }

    public IEnumerable<string> Countries { get; set; }

    [DisplayName("Business")]
    [Required(ErrorMessage = "Please select a business unit.")]
    public string BusinessUnit { get; set; }

    public IEnumerable<string> Businesses { get; set; }

    public Boolean ToGetFit { get; set; }
    public Boolean ToChallengeMyself { get; set; }      
    public Boolean ToBeHealthier { get; set; }
    public Boolean ChallengeOther { get; set; }
    public string OtherString { get; set; }

    public void build()
    {
        var myService = new SurveyService(new SurveyRepository());
        Name = myService.getName();
        Email = myService.getEmail();
    }      

当我的构建方法被调用时,将信息导入我的 ViewModel 的最佳方式是什么? 我应该使用 IEnumerable 还是只使用字符串?

这是我的 .aspx 页面上的代码。

<li>    What country do you live in? <br />   
    <%= Html.RadioButtonFor(model => model.Country, "Canada", true) %> Ecuador<br />   
    <%= Html.RadioButtonFor(model => model.Country, "Chile", true) %> Ghana   <br />
    <%= Html.RadioButtonFor(model => model.Country, "Italy", true) %> Nigeria   <br />
    <%= Html.RadioButtonFor(model => model.Country, "Germany", true) %> Romania   
</li>

<li> What business unit do you work in?<br />
    <%= Html.RadioButtonFor(model => model.BusinessUnit, "Pharmaceuticals", true ) %> Pharmaceuticals <br />        
    <%= Html.RadioButtonFor(model => model.BusinessUnit, "Mechanics", true) %> Vaccines  <br />
    <%= Html.RadioButtonFor(model => model.BusinessUnit, "R&D") %> R&D   <br />
    <%= Html.RadioButtonFor(model => model.BusinessUnit, "Distribution", true) %> Distribution   <br />
</li>           

<li>    Why do you want to take part in this workout? <br />
    <%= Html.CheckBoxFor(model => model.ToGetFit ) %>   To get in shape   <br />
    <%= Html.CheckBoxFor(model => model.ToChallengeMyself ) %> To challenge myself     <br />       
    <%= Html.CheckBoxFor(model => model.ToBeHealthier) %>   To be healthier     <br />
    <%= Html.CheckBoxFor(model => model.ChallengeOther) %>  Other  
    <%= Html.TextBoxFor(model => model.OtherString) %>
</li>

我是 ASP.NET MVC 的新手,但我对 ASP.NET 和 MVC 模式都有经验。 我希望尽可能多地强调关注点的分离。

我的 Controller 类调用我的构建方法。我有一个服务类,它获取一个存储库对象,该对象将从我的模型/数据库中获取信息。

!我希望我的单选按钮动态地从我的数据库中获取。然后,如果用户已经从上一个 session 中选择了一些东西,我希望在我的 ViewModel 中找到它,并且在他们加载页面时已经选择了它。

最佳答案

我在您的上述方法中看到的问题是您的单选按钮和复选框是在您的 View 中定义的。如果它们来自数据库或枚举或其他东西,这显然是一个问题。您应该做的是添加一个 ViewModel 属性,该属性表示给定单选/复选框组的所有可能选项,以及一个保存用户为该组选择的选定值的属性。

首先,检查“CheckBoxListFor”和“RadioButtonListFor”的这些GREAT 辅助方法。将它们复制到您的项目中。

接下来,您需要在 ViewModel 上将您的集合公开为 IEnumerable<SelectListItem> .这些是您的 radiolist/checkboxlist/dropdown 元素的可能选项。

然后,您的 ViewModel 需要为单个选定选项(来自下拉列表或单选按钮列表)或选定选项的集合(来自复选框列表)定义 Prop 。您可以使用项目数据库 ID 中的 int/string,或者如果要选择的类型是枚举,您可以直接绑定(bind)到它。

最后,您的 View 变得 super 简单,如果您添加新选项,则无需改动。对于数据库驱动的选项,根本不需要更改任何代码。

下面是我打开的演示页面的示例。我相信你应该能够看到它并将其应用到你自己的项目中。

ViewModel:

public class OrderViewModel
    {
        public int FavoriteMovieID { get; set; }
        public List<int> MovieIdsForMoviesILike { get; set; }

        public MovieCategories FavoriteMovieType { get; set; }
        public List<MovieCategories> MovieCategoriesILike { get; set; }

        public IEnumerable<SelectListItem> MoviesToSelectFrom 
        {
            get
            {
                return from x in OrderDetailsRepo.GetAllMovies()
                       select new SelectListItem {
                           Text = x.Title,
                           Value = x.ID.ToString(),
                           Selected = MovieIdsForMoviesILike.Contains(x.ID),
                       };
            }
        }

        public IEnumerable<SelectListItem> MovieCategoriesToSelectFrom
        {
            get
            {
                return from cat in Enum.GetValues(typeof(MovieCategories)).Cast<MovieCategories>()
                       select new SelectListItem {
                           Text = cat.ToString(),
                           Value = cat.ToString(),
                           Selected = MovieCategoriesILike.Contains(cat),
                       };
            }
        }

        public OrderViewModel()
        {
            // to ensure they are never NULL
            MovieIdsForMoviesILike = new List<int>();
            MovieCategoriesILike = new List<MovieCategories>();
        }
}

域模型类和提供集合的方法:

 public static class OrderDetailsRepo
        {
            public static List<Movie> GetAllMovies()
            {
                return new List<Movie> {
                    new Movie { ID = 0, Title = "Great Expectation" },
                    new Movie { ID = 1, Title = "Gone with the Wind" },
                    new Movie { ID = 2, Title = "Lion of Winter" },
                };
            }
        }

    public class Movie
        {
            public string Title { get; set; }
            public int ID { get; set; }
        }

    public enum MovieCategories
        {
            Horror,
            Drama,
            Comedy,
        }

和 super 简化的 View :

@model MVC3App.ViewModels.OrderViewModel
@using MVC3App.MVCHtmlHelpers @* RadioButtonList and CheckBoxList are defined in here *@

@{
    ViewBag.Title = "ViewPage1";
}

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <div class="editor-label">
        What's your favorite movie?
    </div>
    <div class="editor-field">
        @Html.RadioButtonListFor(model => model.FavoriteMovieID, Model.MoviesToSelectFrom)
        @Html.ValidationMessageFor(model => model.FavoriteMovieID)
    </div>

    <div class="editor-label">
        What movies do you like in general?
    </div>
    <div class="editor-field">
        @Html.CheckboxListFor(model => model.MovieIdsForMoviesILike, Model.MoviesToSelectFrom)
        @Html.ValidationMessageFor(model => model.MovieIdsForMoviesILike)
    </div>

    <div class="editor-label">
        What's your favorite Movie Genre?
    </div>
    <div class="editor-field">
        @Html.RadioButtonListFor(model => model.FavoriteMovieType, Model.MovieCategoriesToSelectFrom)
        @Html.ValidationMessageFor(model => model.FavoriteMovieType)
    </div>

    <div class="editor-label">
        What Movie Genres do you like in general?
    </div>
    <div class="editor-field">
        @Html.CheckboxListFor(model => model.MovieCategoriesILike, Model.MovieCategoriesToSelectFrom)
        @Html.ValidationMessageFor(model => model.MovieCategoriesILike)
    </div>

    <br /><br />
    <input type="submit" value="Save" />
}

关于c# - 我应该如何构建我的 View 模型 - ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14762680/

相关文章:

c# - 为什么我的 WPF 应用程序会在我碰撞鼠标滚轮时崩溃?

c# - 设置数据绑定(bind) DropDownList 的 SelectedValue

c# - StructureMap:选择嵌套依赖的具体类型

html - 替换由 html.displayfor(function) 生成的文本中的字符

asp.net-mvc-3 - ASP.NET MVC3 中带有 Ninject 的 RavenDB

jquery - 从cshtml中通过JQuery调用Controller中的Action并返回一个值

c# - 重新抛出 ex.InnerException 是个坏主意?

javascript - CheckBox 选中所有子复选框

c# - 如何使用一个动态生成的对象作为CodeEffects生成器的数据源

c# - HttpWebRequest.GetResponse() 返回 404 错误