asp.net-mvc-2 – 带有数组/列表的ASP.NET MVC 2模型
发布时间:2020-10-19 06:53:52 所属栏目:asp.Net 来源:互联网
导读:我正在ASP.NET MVC中创建我的第一个站点,这是一种学习的方式.但我遇到了一个我无法找到解决方案的问题. 我希望我的用户能够创建附有歌曲和标签的专辑. 这可能是一个未指定数量的歌曲和标签.但必须至少有5首歌曲和2个标签. 但我无法弄清楚如何通过模型实现这一
我正在ASP.NET MVC中创建我的第一个站点,这是一种学习的方式.但我遇到了一个我无法找到解决方案的问题. 我希望我的用户能够创建附有歌曲和标签的专辑. 但我无法弄清楚如何通过模型实现这一目标,这是我能够走多远. public class AlbumCreateModel { [Required] [DisplayName("Title")] public string Title { get; set; } [DisplayName("Description")] public string Description { get; set; } [DisplayName("Publish")] public bool Public { get; set; } [DisplayName("Tags")] // Min 2 tags no max public List<AlbumTagModel> Tags { get; set; } [DisplayName("Songs")] // Min 5 songs no max public List<AlbumSongModel> Songs { get; set; } } public class AlbumTagModel { [Required] [DisplayName("Tag")] // Regex to test no spaces // min 2 characters // maximum 15 characters public string Tag { get; set; } } public class AlbumSongModel { [Required] [DisplayName("Title")] public string Title { get; set; } [Required] [DisplayName("Artist")] public string Artist { get; set; } [DisplayName("Description")] public string Description { get; set; } [DisplayName("Song Length")] public double Length { get; set; } [DisplayName("Year")] public int Description { get; set; } } 视图: <%@ Page Title="" Language="C#" MasterPageFile="~/App/Views/Shared/MasterPage.Master" Inherits="System.Web.Mvc.ViewPage<album.App.Models.AlbumCreateModel>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Create </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <% using (Html.BeginForm()) { %> <%: Html.ValidationSummary(true,"Committing the album was unsuccessful. Please correct the errors and try again.")%> <div> <fieldset> <legend>Album Information</legend> <div class="editor-label"> <%: Html.LabelFor(m => m.Title) %> </div> <div class="editor-field"> <%: Html.TextBoxFor(m => m.Title)%> <%: Html.ValidationMessageFor(m => m.Title)%> </div> <div class="editor-label"> <%: Html.LabelFor(m => m.Description) %> </div> <div class="editor-field"> <%: Html.TextAreaFor(m => m.Description)%> <%: Html.ValidationMessageFor(m => m.Description)%> </div> <!-- Tags here --> <!-- Songs here --> <p> <input type="submit" value="Commit" /> </p> </fieldset> </div> <% } %> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="MetaData" runat="server"> </asp:Content> 可能的解决方案: 模型: public class PlaylistModel { [Required] [DisplayName("Title")] public string Title { get; set; } [DisplayName("Description")] public string Description { get; set; } [DisplayName("Publish")] public bool Public { get; set; } [DisplayName("Tags")] [ListCount(Min = 2)] // Min 2 tags no max public List<PlaylistTagModel> Tags { get; set; } [DisplayName("Songs")] [ListCount(Min = 5)] public List<PlaylistSongModel> Songs { get; set; } } public class PlaylistTagModel { [Required] [DisplayName("Tag")] // Regex to test no spaces // min 2 characters // maximum 15 characters public string Tag { get; set; } } public class PlaylistSongModel { [Required] [DisplayName("Title")] public string Title { get; set; } [Required] [DisplayName("Artist")] public string Artist { get; set; } [DisplayName("Description")] public string Description { get; set; } [DisplayName("Song Length")] public int Length { get; set; } [DisplayName("Year")] public int Year { get; set; } } 视图: <%@ Page Title="" Language="C#" MasterPageFile="~/App/Views/Shared/MasterPage.Master" Inherits="System.Web.Mvc.ViewPage<playlist.App.Models.PlaylistModel>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Create </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <% using (Html.BeginForm()) { %> <%: Html.ValidationSummary(true,"Committing the playlist was unsuccessful. Please correct the errors and try again.")%> <div> <fieldset> <legend>Playlist Information</legend> <div class="editor-label"> <%: Html.LabelFor(m => m.Title) %> </div> <div class="editor-field"> <%: Html.TextBoxFor(m => m.Title)%> <%: Html.ValidationMessageFor(m => m.Title)%> </div> <div class="editor-label"> <%: Html.LabelFor(m => m.Description) %> </div> <div class="editor-field"> <%: Html.TextAreaFor(m => m.Description)%> <%: Html.ValidationMessageFor(m => m.Description)%> </div> <br /> <%: Html.ValidationMessageFor(m => m.Tags)%> <div class="editor-label"> <%: Html.LabelFor(m => m.Tags)%> </div> <div class="editor-field"> <%: Html.EditorFor(m => m.Tags) %> <%: Html.Editor("Tags[" + (Model == null ? 0 : Model.Tags.Count) + "]","PlaylistTagModel")%> </div> <br /> <%: Html.ValidationMessageFor(m => m.Songs)%> <div class="editor-label"> <%: Html.LabelFor(m => m.Songs)%> </div> <div class="editor-field"> <%: Html.EditorFor(m => m.Songs)%> <%: Html.Editor("Songs[" + (Model == null ? 0 : Model.Songs.Count) + "]","PlaylistSongModel")%> </div> <p> <input type="submit" value="Commit" /> </p> </fieldset> </div> <% } %> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="MetaData" runat="server"> </asp:Content> 这两个模板: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<playlist.App.Models.PlaylistSongModel>" %> <fieldset> <legend>Song Information</legend> <%: Html.ValidationSummary(true,"Committing this song was unsuccessful. Please correct the errors and try again.")%> <div class="editor-label"> <%: Html.LabelFor(m => m.Title) %> </div> <div class="editor-field"> <%: Html.TextBoxFor(m => m.Title)%> <%: Html.ValidationMessageFor(m => m.Title)%> </div> <div class="editor-label"> <%: Html.LabelFor(m => m.Artist)%> </div> <div class="editor-field"> <%: Html.TextBoxFor(m => m.Artist)%> <%: Html.ValidationMessageFor(m => m.Artist)%> </div> <div class="editor-label"> <%: Html.LabelFor(m => m.Description)%> </div> <div class="editor-field"> <%: Html.TextAreaFor(m => m.Description)%> <%: Html.ValidationMessageFor(m => m.Description)%> </div> <div class="editor-label"> <%: Html.LabelFor(m => m.Length)%> </div> <div class="editor-field"> <%: Html.TextBoxFor(m => m.Length)%> <%: Html.ValidationMessageFor(m => m.Length)%> </div> <div class="editor-label"> <%: Html.LabelFor(m => m.Year)%> </div> <div class="editor-field"> <%: Html.TextBoxFor(m => m.Year)%> <%: Html.ValidationMessageFor(m => m.Year)%> </div> </fieldset> (编辑:岳阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – ASP.net身份在删除外部帐户后停止分发外部C
- asp.net – 适用于Linq To SQL DAL的静态方法吗?
- asp.net-mvc – 如何正确识别vs2008版本级别?
- asp.net-core – ClaimTypes的ASP.NET要求
- asp.net-mvc-3 – 应该如何看待“分离”?
- asp.net-mvc-3 – 如何避免使用MVC3 FileContentResult重复
- asp.net-mvc – Url.Action如何从模型中添加参数值
- asp.net – 如何将两个模型传递给一个View
- asp.net-mvc – 在IIS 5.1上部署ASP.NET MVC(Windows XP)
- 发布一款层次下拉列表控件
推荐文章
站长推荐
- asp.net – Lucene.Net和孵化状态
- asp.net-mvc – 为什么ASP.NET MVC使用会话状态?
- 并行运行ASP.NET Webforms和ASP.NET MVC
- asp.net读取excel文件的三种方法示例
- asp.net-mvc – 为什么MVC4捆绑捆绑Knockout.js?
- asp.net-mvc – 根据浏览器接受语言自动设置uiCu
- asp.net-core – ASP.Net核心maxUrlLength
- webforms – ASP.Net Core 1.0是否支持WebForm项
- asp.net-mvc-3 – MVC 3 – Html.EditorFor似乎缓
- asp.net-mvc – Url.Action如何从模型中添加参数
热点阅读