Asp.net mvc 访问IEnumerable值列表
我有以下代码Asp.net mvc 访问IEnumerable值列表,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我有以下代码 @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="ma
@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>
@model IEnumerable
#产品总数
{
宽度:20px;
左边距:10px;
}
@使用(Html.BeginForm())
{
项目
细枝末节
数量
小计
删除项目
@foreach(模型中的var项目)
{
@DisplayFor(x=>item.Model\u Name)
30天更换
2-3个工作日内免费送货。
@文本框(“总产品”,1)
Rs.@Html.DisplayFor(x=>item.Price)
去除
}
估计价格:卢比。@ViewBag.total
}
这是控制器
public ActionResult Order(IEnumerable<Product> products, string Action)
{
if (Action == "PLACE ORDER")
{
//some code here
}
公共操作结果顺序(IEnumerable产品、字符串操作)
{
如果(操作==“下订单”)
{
//这里有一些代码
}
产品对象
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; }
}
公共部分类乘积
{
公共int Id{get;set;}
公共字符串模型_Name{get;set;}
公共字符串公司名称{get;set;}
公共可空价格{get;set;}
公共字符串Photo{get;set;}
公共字符串Big_Photo{get;set;}
公共字符串AlternateText{get;set;}
公共字符串模型\u全名{get;set;}
公共可空乘积_Id{get;set;}
公共字符串视频_记录{get;set;}
公共字符串second_照相机{get;set;}
公共字符串主\u摄影机{get;set;}
公共字符串触摸屏{get;set;}
公共字符串操作系统{get;set;}
公共字符串保证{get;set;}
公共可为空的公司_Id{get;set;}
公共可为空的Total_乘积{get;set;}
}
当我尝试访问IEnumerable值时,我得到的是null。我得到的是Action变量的值。我甚至尝试使用for循环,但仍然不起作用。我能够显示。这是我问的基本问题,但我尝试在Google上搜索,但无法更正我的错误。有人能告诉我如何更正abov吗e code.谢谢为了让表单在POST中提交默认的MVC模型绑定机制,您需要确保字段名称在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>
}
(变量i=0;i<10;i++)
{
} 您还有几个其他选择:
另外,您可能需要更改
操作
变量的名称,以避免与混淆。能否显示您的产品对象?您是否尝试过使用chrome控制台?@NachoLaborde您能告诉我哪里出了问题吗?制作模型IList
,并使用带有强类型帮助的for
循环ers-for(inti=0;im.Total_产品)…}
AFAIK您不能通过重写元素的名称
,您能给我推荐一个构建Json数组的示例吗?我尝试过使用@for循环,但没有结果。谢谢alot@User,如果您的for循环不起作用,则说明您犯了其他错误-使用您使用的代码编辑您的问题。