大佬教程收集整理的这篇文章主要介绍了asp.net – 用于DropDownList的MVC2 EditorTemplate,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
以下是我提出的一种方法,并希望对这种方法提出意见:
这是我的设计目标:
>视图模型应包含下拉列表的源数据
>限制Sil弦
>不使用ViewData字典
>控制器负责使用下拉列表的源数据填充属性
这是我的查看型号:
public class CustomerViewModel { [Scaffoldcolumn(false)] public String CustomerCode{ get; set; } [UIHint("DropDownList")] [DropDownList(DropDownListTargetProperty = "CustomerCode"] [DisplayName("Customer Code")] public IEnumerable<SELEctListItem> CustomerCodeList { get; set; } public String FirstName { get; set; } public String LastName { get; set; } public String Phonenumber { get; set; } public String Address1 { get; set; } public String Address2 { get; set; } public String City { get; set; } public String State { get; set; } public String Zip { get; set; } }
我的视图模型具有CustomerCode属性,它是用户从值列表中选择的值。我有一个CustomerCodeList属性,它是可能的CustomerCode值的列表,并且是下拉列表的来源。我已经使用DropDownListTargetProperty创建了一个DropDownList属性。 DropDownListTargetProperty指向将根据生成的下拉列表中的用户选择(在本例中为CustomerCode属性)填充的属性。
请注意,CustomerCode属性具有[Scaffoldcolumn(false)],这迫使发生器跳过生成的输出中的字段。
我的DropDownList.ascx文件将生成一个来自CustomerCodeList属性的源数据的下拉列表表单元素。生成的下拉列表将使用DropDownList属性中的DropDownListTargetProperty的值作为Id和SELEct表单元素的Name属性。所以生成的代码将如下所示:
<SELEct id="CustomerCode" name="CustomerCode"> <option>... </SELEct>
这样做非常好,因为在提交表单时,MVC将从下拉列表中选择所需值填充目标属性,因为生成的下拉列表的名称是目标属性。我有点可视化,因为CustomerCodeList属性是CustomerCode属性的扩展。我将源数据与资源相结合。
这是我的控制器代码:
public ActionResult Create() { //retrieve CustomerCodes from a datasource of your choosing List<CustomerCode> customerCodeList = modelservice.GetCustomerCodeList(); CustomerViewModel viewModel= new CustomerViewModel(); viewModel.CustomerCodeList = customerCodeList.SELEct(s => new SELEctListItem() { Text = s.CustomerCode,Value = s.CustomerCode,SELEcted = (s.CustomerCode == viewModel.CustomerCodE) }).AsEnumerable(); return View(viewModel); }
这是我的DropDownListAttribute的代码:
namespace AutoForm.Attributes { public class DropDownListAttribute : Attribute { public String DropDownListTargetProperty { get; set; } } }
这是我的模板代码(DropDownList.ascX):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<SELEctListItem>>" %> <%@ Import Namespace="AutoForm.Attributes"%> <script runat="server"> DropDownListAttribute GetDropDownListAttribute() { var dropDownListAttribute = new DropDownListAttribute(); if (ViewData.modelMetadata.AdditionalValues.ContainsKey("DropDownListAttribute")) { dropDownListAttribute = (DropDownListAttributE)ViewData.modelMetadata.AdditionalValues["DropDownListAttribute"]; } return dropDownListAttribute; } </script> <% DropDownListAttribute attribute = GetDropDownListAttribute();%> <SELEct id="<%= attribute.DropDownListTargetProperty %>" name="<%= attribute.DropDownListTargetProperty %>"> <% foreach(SELEctListItem item in ViewData.model) {%> <% if (item.SELEcted == truE) {%> <option value="<%= item.Value %>" SELEcted="true"><%= item.Text %></option> <% } %> <% else {%> <option value="<%= item.Value %>"><%= item.Text %></option> <% } %> <% } %> </SELEct>
我尝试使用Html.DropDownList帮助器,但是不允许我更改生成的SELEct元素的Id和Name属性。
注意:您必须覆盖DropDownListAttribute的DataAnnotationsModelMetadataProvider的CreateMetadata方法。这是代码:
public class MetadataProvider : DataAnnotationsModelMetadataProvider { protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes,Type containerType,Func<object> modelAccessor,Type modelType,String propertyName) { var metadata = base.CreateMetadata(attributes,containerType,modelAccessor,modelType,propertyName); var additionalValues = attributes.OfType<DropDownListAttribute>().FirstOrDefault(); if (additionalValues != null) { metadata.AdditionalValues.Add("DropDownListAttribute",additionalValues); } return metadata; } }
那么你必须在Global.asax.cs的Application_Start中调用新的MetadataProvider:
protected void Application_Start() { RegisterRoutes(RouteTable.Routes); ModelMetadataProviders.Current = new MetadataProvider(); }
那么,我希望这是有道理的,我希望这种方法可以节省你一些时间。我想要一些关于这种方法的反馈。有更好的方法吗?
这是我的Object.ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <%@ Import Namespace="WebAppSolutions.Helpers" %> <% if (ViewData.TemplateInfo.TemplateDepth > 1) { %> <%= ViewData.modelMetadata.SimpleDisplayText%> <% } else { %> <% foreach (var prop in ViewData.modelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) { %> <% var htmlFieldName = Html.HtmlFieldNameFor(prop.propertyName);%> <% if (prop.HideSurroundingHtml) { %> <%= Html.Editor(htmlFieldName)%> <% } else { %> <div id="<%= htmlFieldName %>Container" class="editor-field"> <% if (!String.IsNullOrEmpty(Html.Label(prop.propertyName).ToHtmlString())) { %> <%= Html.Label(prop.propertyName,Html.HtmlDisplayName(prop.propertyName),prop.Isrequired)%> <% } %> <%= Html.Editor(prop.propertyName,"",htmlFieldName)%> <%= Html.Validationmessage(prop.propertyName,"*") %> </div> <% } %> <% } %>
<%}%>
我的WebAppSolutions.Helpers中有一些托管代码用于HtmlFieldNameFor和HtmlDisplayName。这些帮助程序从应用于视图模型中的属性的属性中检索数据。
public static String HtmlFieldNameFor<TModel>(this HtmlHelper<TModel> html,String propertyName) { ModelMetadata modelMetaData = GetModelMetaData(html,propertyName); return GetHtmlFieldName(modelMetaData,propertyName); } public static String HtmlDisplayName<TModel>(this HtmlHelper<TModel> html,propertyName); return modelMetaData.DisplayName ?? propertyName; } private static ModelMetadata GetModelMetaData<TModel>(HtmlHelper<TModel> html,String propertyName) { ModelMetadata modelMetaData = ModelMetadata.FromStringExpression(propertyName,html.ViewData); return modelMetaData; } private static String GetHtmlFieldName(ModelMetadata modelMetaData,String defaultHtmlFieldName) { PropertyExtendedMetaDataAttribute propertyExtendedMetaDataAttribute = GetPropertyExtendedMetaDataAttribute(modelMetaData); return propertyExtendedMetaDataAttribute.HtmlFieldName ?? defaultHtmlFieldName; }
使用EditorModelFor()得到这个工作的关键是这个(在上面的Object.ascx中应该是第20行):
<%= Html.Editor(prop.propertyName,htmlFieldName)%>
prop.propertyName是ViewModel中的属性,包含将成为DropDownList的数据列表。 htmlFieldName是DropDownList属性替换隐藏的属性的名称。合理?
我希望这可以帮助你。
以上是大佬教程为你收集整理的asp.net – 用于DropDownList的MVC2 EditorTemplate全部内容,希望文章能够帮你解决asp.net – 用于DropDownList的MVC2 EditorTemplate所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。