Given the following class, what is your opinion on the best way to handle create/edit where Attributes.Count can be any number.
鉴于以下类,您对处理创建/编辑的最佳方法有何看法,其中Attributes.Count可以是任何数字。
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;}
}
The user should be able to edit both the Product details (Name) and Attribute details (Name/Value) in the same view, including adding and deleting new attributes.
用户应该能够在同一视图中编辑产品详细信息(名称)和属性详细信息(名称/值),包括添加和删除新属性。
Handling changes in the model is easy, what's the best way to handle the UI and ActionMethod side of things?
处理模型中的更改很容易,处理UI和ActionMethod方面的最佳方法是什么?
5 个解决方案
#1
3
Use the FormCollection and iterate through the key/value pairs. Presumably you can use a naming scheme that will allow you to determine which key/value pairs belong to your attribute set.
使用FormCollection并遍历键/值对。据推测,您可以使用命名方案来确定哪些键/值对属于您的属性集。
[AcceptVerbs( HttpVerb.POST )]
public ActionResult Whatever( FormCollection form )
{
....
}
#2
14
Look at Steve Sanderson’s blog post Editing a variable length list, ASP.NET MVC 2-style.
请看Steve Sanderson的博客文章编辑可变长度列表,ASP.NET MVC 2样式。
Controller
Your action method receives your native domain model Product
and stays pretty simple:
您的操作方法接收您的本机域模型产品并保持非常简单:
public ActionResult Edit(Product model)
View
Edit.aspx
Edit.aspx
<!-- Your Product inputs -->
<!-- ... -->
<!-- Attributes collection edit -->
<% foreach (Attribute attr in Model.Attributes)
{
Html.RenderPartial("AttributeEditRow", attr);
} %>
AttributeEditRow.ascx
AttributeEditRow.ascx
Pay your attention to helper extension Html.BeginCollectionItem(string)
注意帮助扩展Html.BeginCollectionItem(string)
<% using(Html.BeginCollectionItem("Attributes")) { %>
<!-- Your Attribute inputs -->
<% } %>
Adding and editing of new attributes is possible too. See the post.
也可以添加和编辑新属性。看帖子。
#3
3
Use a custom Model Binder, and write the Action methods as you would normally:
使用自定义Model Binder,并像平常一样编写Action方法:
ActionResult Edit(
int id,
[ModelBinder(typeof(ProductModelBinder))] Product product
) ...
In your ProductModelBinder, you iterate over the Form Collection values and bind to a Product entity. This keeps the Controller interface intuitive, and can help testing.
在ProductModelBinder中,迭代Form Collection值并绑定到Product实体。这使控制器界面更直观,并可以帮助测试。
class ProductModelBinder : IModelBinder ...
#4
0
Depends on the experience you are looking to create for the user. I have implemented something similar for tagging content. In the model, Tags are represented as IList, but the UI shows a comma delimited list in a single text field. I then handle merging the items in the list into a string to populate the text field, and I split the input to put items back into the IList in the model.
取决于您希望为用户创建的体验。我已经为标记内容实现了类似的功能。在模型中,标签表示为IList,但UI在单个文本字段中显示逗号分隔的列表。然后我处理将列表中的项合并为一个字符串以填充文本字段,然后我将输入拆分为将项放回模型中的IList。
In my DAL, I then deal with converting the List into LINQ entities, handle inserts and deletes, etc.
在我的DAL中,我接着将List转换为LINQ实体,处理插入和删除等。
It isn't the most straight forward code, but it isn't too difficult to manage and it gives the user an expected interface.
它不是最直接的代码,但它不是很难管理,它为用户提供了一个预期的界面。
I'm sure there are other ways to handle it but I would focus on what would work best for the user and then work out the mapping details based on that.
我确信还有其他方法可以处理它,但我会专注于对用户最有效的方法,然后根据它来计算映射细节。
#5
0
Andrew,
安德鲁,
I'm thinking something a little more difficult than tags. In this simple case a name / value pair .. color: Red; size: 10; material: cotton.
我在想一些比标签更难的东西。在这个简单的例子中,名称/值对..颜色:红色;大小:10;材质:棉。
I think anything that could be used on that could extend to more complex. I.e. Adding a category and adding all its items on the same page. It's relatively easy to add another line using some jQuery, but what's the consensus on sending the info to the ActionMethod?
我认为任何可以使用的东西都可以扩展到更复杂的东西。即添加类别并在同一页面上添加其所有项目。使用一些jQuery添加另一行相对容易,但是将信息发送到ActionMethod的共识是什么?
You can't code:
你不能编码:
public ActionResult Whatever(stirng attr1Name, string attr2Name, string attr3Name ...
Also I don't think accepting this would work either:
我也不认为接受这个也可以:
public ActionResult Whatever(ILIst<Attribute> attributes, string productName ...
#1
3
Use the FormCollection and iterate through the key/value pairs. Presumably you can use a naming scheme that will allow you to determine which key/value pairs belong to your attribute set.
使用FormCollection并遍历键/值对。据推测,您可以使用命名方案来确定哪些键/值对属于您的属性集。
[AcceptVerbs( HttpVerb.POST )]
public ActionResult Whatever( FormCollection form )
{
....
}
#2
14
Look at Steve Sanderson’s blog post Editing a variable length list, ASP.NET MVC 2-style.
请看Steve Sanderson的博客文章编辑可变长度列表,ASP.NET MVC 2样式。
Controller
Your action method receives your native domain model Product
and stays pretty simple:
您的操作方法接收您的本机域模型产品并保持非常简单:
public ActionResult Edit(Product model)
View
Edit.aspx
Edit.aspx
<!-- Your Product inputs -->
<!-- ... -->
<!-- Attributes collection edit -->
<% foreach (Attribute attr in Model.Attributes)
{
Html.RenderPartial("AttributeEditRow", attr);
} %>
AttributeEditRow.ascx
AttributeEditRow.ascx
Pay your attention to helper extension Html.BeginCollectionItem(string)
注意帮助扩展Html.BeginCollectionItem(string)
<% using(Html.BeginCollectionItem("Attributes")) { %>
<!-- Your Attribute inputs -->
<% } %>
Adding and editing of new attributes is possible too. See the post.
也可以添加和编辑新属性。看帖子。
#3
3
Use a custom Model Binder, and write the Action methods as you would normally:
使用自定义Model Binder,并像平常一样编写Action方法:
ActionResult Edit(
int id,
[ModelBinder(typeof(ProductModelBinder))] Product product
) ...
In your ProductModelBinder, you iterate over the Form Collection values and bind to a Product entity. This keeps the Controller interface intuitive, and can help testing.
在ProductModelBinder中,迭代Form Collection值并绑定到Product实体。这使控制器界面更直观,并可以帮助测试。
class ProductModelBinder : IModelBinder ...
#4
0
Depends on the experience you are looking to create for the user. I have implemented something similar for tagging content. In the model, Tags are represented as IList, but the UI shows a comma delimited list in a single text field. I then handle merging the items in the list into a string to populate the text field, and I split the input to put items back into the IList in the model.
取决于您希望为用户创建的体验。我已经为标记内容实现了类似的功能。在模型中,标签表示为IList,但UI在单个文本字段中显示逗号分隔的列表。然后我处理将列表中的项合并为一个字符串以填充文本字段,然后我将输入拆分为将项放回模型中的IList。
In my DAL, I then deal with converting the List into LINQ entities, handle inserts and deletes, etc.
在我的DAL中,我接着将List转换为LINQ实体,处理插入和删除等。
It isn't the most straight forward code, but it isn't too difficult to manage and it gives the user an expected interface.
它不是最直接的代码,但它不是很难管理,它为用户提供了一个预期的界面。
I'm sure there are other ways to handle it but I would focus on what would work best for the user and then work out the mapping details based on that.
我确信还有其他方法可以处理它,但我会专注于对用户最有效的方法,然后根据它来计算映射细节。
#5
0
Andrew,
安德鲁,
I'm thinking something a little more difficult than tags. In this simple case a name / value pair .. color: Red; size: 10; material: cotton.
我在想一些比标签更难的东西。在这个简单的例子中,名称/值对..颜色:红色;大小:10;材质:棉。
I think anything that could be used on that could extend to more complex. I.e. Adding a category and adding all its items on the same page. It's relatively easy to add another line using some jQuery, but what's the consensus on sending the info to the ActionMethod?
我认为任何可以使用的东西都可以扩展到更复杂的东西。即添加类别并在同一页面上添加其所有项目。使用一些jQuery添加另一行相对容易,但是将信息发送到ActionMethod的共识是什么?
You can't code:
你不能编码:
public ActionResult Whatever(stirng attr1Name, string attr2Name, string attr3Name ...
Also I don't think accepting this would work either:
我也不认为接受这个也可以:
public ActionResult Whatever(ILIst<Attribute> attributes, string productName ...