我有以下代码
@model IEnumerable<Shop_Online.com.Models.Product>
<style type="text/css">
#Total_Products
{
width:20px;
margin-left:10px;
}
</style>
@using (Html.BeginForm())
{
<div style="width: 860px; margin: 0 auto" class="main">
<table border="1" style="font-family: Verdana; font-size: 13px">
<tr style="background-color: #f2f2f2;height:30px">
<th colspan="4">ITEM</th>
<th>DELIEVERY DETAILS</th>
<th>QTY</th>
<th>SUB TOTAL</th>
<th>Remove Items</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td colspan="4" style="width: 46%">
<table style="font-family: Verdana; font-size: 13px">
<tr>
<td>
<img src="@Url.Content(item.Photo)" alt="Image" style="width:36px" />
</td>
<td>
@Html.DisplayFor(x => item.Model_Name)
</td>
</tr>
<tr>
<td style="color: #ccc">30 days Replacement</td>
</tr>
</table>
</td>
<td style="width: 39%">Free Delievery Delivered in 2-3 business days.</td>
<td style="width: 5%">@Html.TextBox("Total_Products",1)</td>
<td style="width: 50%"><b>Rs. @Html.DisplayFor(x => item.Price)</b></td>
<td><button type="submit" class="remove" value="@item.Id" id="@item.Id" name="Action">Remove</button></td>
</tr>
}
</table>
<div style="width: 100%; height: 70px; background-color: #f2f2f2">
<div style="width: 75%; height: 70px; float: left; font-family: Verdana; font-size: 13px">
</div>
<div style="width: 25%; height: 70px; float: left; font-family: Verdana; padding-top: 20px; font-size: 13px">
Estimated Price: <b>Rs.@ViewBag.total</b>
</div>
</div>
<div class="order" style="width: 100%; height: 70px">
<div class="left-order" style="width: 75%; height: 70px; float: left"></div>
<div class="left-order" style="width: 25%; float: left; height: 70px">
<input type="submit" name="Action" value="PLACE ORDER" style="border: 1px solid #ec6723; width: 216px; cursor: pointer; height: 45px; color: #fff; background: -webkit-linear-gradient(top,#f77219 1%,#fec6a7 3%,#f77219 7%,#f75b16 100%)" />
</div>
</div>
</div>
}
这是控制器
public ActionResult Order(IEnumerable<Product> products, string Action)
{
if (Action == "PLACE ORDER")
{
//some code here
}
产品对象
public partial class Product
{
public int Id { get; set; }
public string Model_Name { get; set; }
public string Company_Name { get; set; }
public Nullable<int> Price { get; set; }
public string Photo { get; set; }
public string Big_Photo { get; set; }
public string AlternateText { get; set; }
public string Model_FullName { get; set; }
public Nullable<int> Product_Id { get; set; }
public string Video_record { get; set; }
public string second_camera { get; set; }
public string primary_camera { get; set; }
public string touch_screen { get; set; }
public string operating_sys { get; set; }
public string warranty { get; set; }
public Nullable<int> Company_Id { get; set; }
public Nullable<int> Total_Products { get; set; }
}
当我尝试访问IEnumerable值时,我变为null。我正在为Action变量获得价值。我甚至尝试使用for循环,但它仍然无效。我可以显示。这是我要问的基本内容,但我尝试在谷歌上搜索但却无法纠正我的错误。任何人都可以告诉我如何纠正上述代码。感谢
答案 0 :(得分:1)
为了使POST中的表单提交到recognize a collection of entities的默认MVC模型绑定机制,您需要确保{{1}中字段的名称具有基于零的运行索引返回控制器的Html元素的属性,例如:
name
您还有其他几种选择:
另外,您可能希望更改@for (var i = 0; i < 10; i ++)
{
<input type='text' name="products[@i].ProductId"/>
<input type='text' name="products[@i].Total_Products">
<br>
}
变量的名称,以避免与Linq Action delegate混淆