有些例子希望澄清可能的情况:
If I am adding a product to category
Toys with the Target Market USA I need
to ask for the “Age range” and “Did
it pass the safety inspection”.If I am adding a product to category
Toys with Target Market Mexico,I just
need to ask for “Age range”.If I am adding a product to the
category Clothing with the Target
Market USA I need to ask for the
“Style” and “Material”If I am adding a product to the
category Clothing with the Target
Market Canada I need to ask for the
“Style” and “Material” and “USA Price”We have 20 categories and 12 Target
Markets,plus there are 10 forms that
need to behave in this fashion,so in
theory there are 2400 distinct
Actions/Views/Models
所以问题是,在ASP.NET MVC中,什么是处理显示所有这些动态表单和处理发送到操作的数据变体的最好方式?
编辑
对产品属性的确定如何确定:它们基于属于市场中的类别的产品的层次结构.例如,它不是我们要求的所有玩具属性和美国属性的添加,它是在美国市场上销售的玩具的属性.在美国销售的玩具需要“安全检查”信息,但美国的服装没有.墨西哥的玩具也不需要“安全检查”信息,因此所有玩具或所有美国产品都不是固有属性,而是类别和市场的组合.
解决方法
public enum AttributeTypeEnum { Currency,Range,List,Number,Text,Boolean } public interface class IAttribute { int Id { get; set; } string Name { get; set; } AttributeTypeEnum AttType { get; set; } } public abstract class BaseAttribute { int Id { get;set;} string Name { get;set;} AttributeTypeEnum AttType { get; set; } } public class RangeAttribute<T> : BaseAttribute { T StartValue { get;set; } T EndValue { get; set; } }
然后将每个属性关联到一个或多个类别
public class CategoryAttribute { int Id { get; set; } IAttribute Attribute { get; set; } }
然后可以根据每个类别列出属性列表
public class CategoryAttributeService() { public IList<CategoryAttributes> GetAttributes(int CategoryId) { return new IList<CategoryAttributes>(); } }
然后,您的控制器可以在ViewData.Model中返回这些属性的列表.
// controller action public class CategoryAttributeController : Controller { public ActionResult CategoryAttributes(int categoryId) { CategoryAttributeService cas = new CategoryAttributeServices(); ViewData.Model = new CategoryAttributeViewData(categoryId) { Attributes = cas.GetAttributes(categoryId); }; return View(); } }
并且让您的视图处理每个项目的类型,并相应地更改每个项目的表单控件/显示,即(具有开始和结束值的范围)布尔值将具有复选框,材料可能是列表框等.
您有如何处理渲染的许多选择,您可以为每个属性类型创建一个单独的.ascx控件来生成表单控件,或者如下创建一个html帮助器方法
<%@ Page Title="" Language="C#" Inherits="ViewPage<CategoryAttributeViewData>" %> <% foreach(CategoryAttribute attribute in ViewData.Model.Attributes) { %> <%= Html.RenderAttribute(attribute) %> <% } %>
和辅助方法如
public static string RenderAttribute(this HtmlHelper,ICategoryAttribute att) { StringWriter stringWriter = new StringWriter(); using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter)) { switch(att.AttributeType) { case AttributeDataType.Boolean: CreateCheckBox(writer,att); break; case AttributeDataType.List: CreateListBox(writer,att); break; // Other types } } stringWriter.ToString(); }
编辑:我已经离开市场了,所以如果我明白这一点,每个市场都有一些类别(一对多)说美国和服装.
类别服装可以出现在许多市场.
每个类别都有一些属性(一对多)(服装:颜色,大小),每个属性可以有很多的市场(一对多)
>市场列表
>类别列表
> MarketCategories列表
> CategoryAttributes的列表
>属性列表
> AttributeMarkets列表
市场>市场分类> CategoryAttributes>属性> AttributeMarkets
那是对的吗?
苹果电脑.