asp.net razor表单提交中的模型绑定动态列表

时间:2022-07-23 16:35:53

I am trying to create an order form for input.

我正在尝试创建一个输入订单。

I need the user to be able to add multiple line items then update. I was trying to use the shopping cart class (where user creates a cart item and adds a list item, multiple lines can be added to the list item).

我需要用户能够添加多个订单项然后更新。我试图使用购物车类(用户创建购物车项目并添加列表项,可以将多行添加到列表项)。

I didn't get through with that. I am using asp.net razor am using webmatrix to build site. Webmatrix is saying that it doesnt recognise Cart().

我没有完全接受。我正在使用asp.net razor使用webmatrix来构建网站。 Webmatrix说它不识别Cart()。

@{ 
if (Session["cart"] == null)
{
Session["cart"] = new Cart();
}
Cart cart = (Cart)Session["cart"];

}
<table id="cartTable">
<tr>
<th class="product">Product</th>
<th class="size">Size</th>
<th class="price">Price</th>
</tr>
@foreach (var item in cart.Items)
{
<tr>
<td class="product">@item.ProductID</td>
<td class="size">@item.Size</td>
<td class="price">£@item.Price</td>
</tr>
}
</table>

Is there a better way to do this? All help is greatly appreciated

有一个更好的方法吗?非常感谢所有帮助

2 个解决方案

#1


2  

There IS a way to bind dynamic list elements

有一种绑定动态列表元素的方法

@foreach (i=0; i< cart.Items.count; i++)
{
<tr>
   <td class="product"> <input type="hidden" name="cart.item[@i].ProductID"> </td>
   <td class="size"> <input type="text" name="cart.item[@i].Size"> </td>
   <td class="price">£ <input type="text" name="cart.item[@i].Price"> </td>
</tr>
}

Webmatrix is saying that it doesnt recognise Cart()

I strongly recommend you place your Models in the Models folder and make them part of the Models namespace. Then they should be available automatically. Otherwise, you may have to reference cart by it's complete reference path (if it is not in your Models folder). example

我强烈建议您将Models放在Models文件夹中,并使它们成为Models命名空间的一部分。然后它们应该自动可用。否则,您可能必须通过它的完整参考路径引用购物车(如果它不在您的Models文件夹中)。例

Datalayer.Entities.Cart cart = (Datalayer.Entities.Cart)Session["cart"];

Final Note: You are not passing your Cart as a Model to your View

最后注意:您没有将购物车作为模型传递给您的视图

example

@model {Project}.Entities.Cart

This is a better practice using the MVC 3 framework. You would have discovered whatever reference problem earlier and would have the option to use tightly bound Helpers

这是使用MVC 3框架的更好实践。您之前会发现任何引用问题,并且可以选择使用紧密绑定的Helpers

#2


0  

I recommend using the Visual Studio 2012 Express over WebMatrix for MVC development. Furthermore, I would also suggest using jQuery (javascript) if you wish to let users add line items on the fly via the same page. I can share an example with you if you like.

我建议使用Visual Studio 2012 Express而不是WebMatrix进行MVC开发。此外,如果您希望让用户通过同一页面动态添加订单项,我还建议您使用jQuery(javascript)。如果你愿意,我可以和你分享一个例子。

One more note: You tagged this with both MVC and WebForms, which are two very different platforms.

还有一点需要注意:你用MVC和WebForms标记了这两个版本,它们是两个非常不同的平台。

Edit:

编辑:

I think Dave A's solution may be better, but to do this with jQuery:

我认为Dave A的解决方案可能会更好,但是要用jQuery做到这一点:

1 Put your add button and hidden div in a form

<form action="/MyController/MyAction" method="post" id="addListItemForm">
<button id="addListItemButton">Add List Item</button>
<div style="hidden">
<input type="text" name="product" id="product" />
<button id="addButton">Add</button>
</div>
<input type="submit" text="Submit" />
</form>

2 Show the form fields on button click

$('#addListItemButton').click(function(){
  $('#addListItemForm div').show();
});

3 Add hidden field on add button click

$('#addButton').click(function(){
  var product = $('#addListItemForm #product').val();
  $("input").attr({
            name : "productListItems[]", 
            type : "hidden",
            value : product
        }).after('#addListItemForm');
});

4 When the form submits, you will have various product names in the productListItems array passed via POST method.

Note: You'll have to play with this a little bit, but it would be a good learning exercise... I am not exactly sure what you are trying to do, but this is my best guess.

注意:你必须稍微玩这个,但这将是一个很好的学习练习...我不确定你想要做什么,但这是我最好的猜测。

#1


2  

There IS a way to bind dynamic list elements

有一种绑定动态列表元素的方法

@foreach (i=0; i< cart.Items.count; i++)
{
<tr>
   <td class="product"> <input type="hidden" name="cart.item[@i].ProductID"> </td>
   <td class="size"> <input type="text" name="cart.item[@i].Size"> </td>
   <td class="price">£ <input type="text" name="cart.item[@i].Price"> </td>
</tr>
}

Webmatrix is saying that it doesnt recognise Cart()

I strongly recommend you place your Models in the Models folder and make them part of the Models namespace. Then they should be available automatically. Otherwise, you may have to reference cart by it's complete reference path (if it is not in your Models folder). example

我强烈建议您将Models放在Models文件夹中,并使它们成为Models命名空间的一部分。然后它们应该自动可用。否则,您可能必须通过它的完整参考路径引用购物车(如果它不在您的Models文件夹中)。例

Datalayer.Entities.Cart cart = (Datalayer.Entities.Cart)Session["cart"];

Final Note: You are not passing your Cart as a Model to your View

最后注意:您没有将购物车作为模型传递给您的视图

example

@model {Project}.Entities.Cart

This is a better practice using the MVC 3 framework. You would have discovered whatever reference problem earlier and would have the option to use tightly bound Helpers

这是使用MVC 3框架的更好实践。您之前会发现任何引用问题,并且可以选择使用紧密绑定的Helpers

#2


0  

I recommend using the Visual Studio 2012 Express over WebMatrix for MVC development. Furthermore, I would also suggest using jQuery (javascript) if you wish to let users add line items on the fly via the same page. I can share an example with you if you like.

我建议使用Visual Studio 2012 Express而不是WebMatrix进行MVC开发。此外,如果您希望让用户通过同一页面动态添加订单项,我还建议您使用jQuery(javascript)。如果你愿意,我可以和你分享一个例子。

One more note: You tagged this with both MVC and WebForms, which are two very different platforms.

还有一点需要注意:你用MVC和WebForms标记了这两个版本,它们是两个非常不同的平台。

Edit:

编辑:

I think Dave A's solution may be better, but to do this with jQuery:

我认为Dave A的解决方案可能会更好,但是要用jQuery做到这一点:

1 Put your add button and hidden div in a form

<form action="/MyController/MyAction" method="post" id="addListItemForm">
<button id="addListItemButton">Add List Item</button>
<div style="hidden">
<input type="text" name="product" id="product" />
<button id="addButton">Add</button>
</div>
<input type="submit" text="Submit" />
</form>

2 Show the form fields on button click

$('#addListItemButton').click(function(){
  $('#addListItemForm div').show();
});

3 Add hidden field on add button click

$('#addButton').click(function(){
  var product = $('#addListItemForm #product').val();
  $("input").attr({
            name : "productListItems[]", 
            type : "hidden",
            value : product
        }).after('#addListItemForm');
});

4 When the form submits, you will have various product names in the productListItems array passed via POST method.

Note: You'll have to play with this a little bit, but it would be a good learning exercise... I am not exactly sure what you are trying to do, but this is my best guess.

注意:你必须稍微玩这个,但这将是一个很好的学习练习...我不确定你想要做什么,但这是我最好的猜测。