Asp.net mvc ASP.NET MVC编辑收藏最佳实践-您的意见
对于下面的类,您对处理创建/编辑where属性的最佳方法有何看法。计数可以是任意数字Asp.net mvc ASP.NET MVC编辑收藏最佳实践-您的意见,asp.net-mvc,collections,Asp.net Mvc,Collections,对于下面的类,您对处理创建/编辑where属性的最佳方法有何看法。计数可以是任意数字 public class Product { public int Id {get;set;} public string Name {get;set;} public IList<Attribute> Attributes {get;set;} } public class Attribute { public string Name {get;set;} public st
public class Product {
public int Id {get;set;}
public string Name {get;set;}
public IList<Attribute> Attributes {get;set;}
}
public class Attribute {
public string Name {get;set;}
public string Value {get;set;}
}
公共类产品{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共IList属性{get;set;}
}
公共类属性{
公共字符串名称{get;set;}
公共字符串值{get;set;}
}
用户应该能够在同一视图中编辑产品详细信息(名称)和属性详细信息(名称/值),包括添加和删除新属性
处理模型中的更改很容易,处理UI和ActionMethod方面的最佳方法是什么?取决于您希望为用户创建的体验。我已经为标记内容实现了类似的功能。在模型中,标记表示为IList,但UI在单个文本字段中显示逗号分隔的列表。然后,我处理将列表中的项目合并为字符串以填充文本字段,并拆分输入以将项目放回模型中的IList中 在我的DAL中,我接着处理将列表转换为LINQ实体、处理插入和删除等问题 它不是最直接的代码,但管理起来也不太困难,它为用户提供了一个预期的界面 我相信还有其他方法可以解决这个问题,但我会专注于什么最适合用户,然后在此基础上制定映射细节。Andrew 我在想一些比标签更难的事情。在这个简单的例子中,名称/值对。。颜色:红色;尺寸:10;材料:棉花 我认为任何可以用在上面的东西都可以扩展到更复杂的领域。即,添加一个类别并在同一页面上添加其所有项目。使用一些jQuery添加另一行相对容易,但是将信息发送到ActionMethod的共识是什么 您无法编写以下代码:
public ActionResult Whatever(stirng attr1Name, string attr2Name, string attr3Name ...
此外,我认为接受这一点也行不通:
public ActionResult Whatever(ILIst<Attribute> attributes, string productName ...
public ActionResult(ILIst属性、字符串productName。。。
使用FormCollection并遍历键/值对。您可能可以使用命名方案来确定哪些键/值对属于您的属性集
[AcceptVerbs( HttpVerb.POST )]
public ActionResult Whatever( FormCollection form )
{
....
}
使用自定义模型活页夹,像通常一样编写操作方法:
ActionResult Edit(
int id,
[ModelBinder(typeof(ProductModelBinder))] Product product
) ...
在ProductModelBinder中,您可以迭代表单集合值并绑定到产品实体。这可以保持控制器界面的直观性,并有助于测试
class ProductModelBinder : IModelBinder ...
请看Steve Sanderson的博客文章 控制器 您的操作方法接收您的本机域模型
产品
,并保持相当简单:
public ActionResult Edit(Product model)
看法
Edit.aspx
<!-- Your Product inputs -->
<!-- ... -->
<!-- Attributes collection edit -->
<% foreach (Attribute attr in Model.Attributes)
{
Html.RenderPartial("AttributeEditRow", attr);
} %>
添加和编辑新属性也是可能的。请参阅帖子。你说得对。我没有考虑对象的集合……我的大脑被当前项目卡住了。我想tvanfosson回复中提到的FormCollection是最好的选择。
<% using(Html.BeginCollectionItem("Attributes")) { %>
<!-- Your Attribute inputs -->
<% } %>