我想从我的视图中传递id值。我正在使用Html.ActionLink属性。 我表现得像这样
@foreach (var item in Model)
{
<div class="grid_1_of_4 images_1_of_4">
<a href="preview.html"><img src="/@item.p_imageUrl" alt="" /></a>
<h2>@item.p_name </h2>
<div class="price-details">
<div class="price-number">
<p><span class="rupees">@item.p_price</span></p>
</div>
@Html.ActionLink("Add to Cart", "Mobiles", new { id = item.ProductId }, new { @class = "add" })
<div class="clear"></div>
</div>
</div>
}
但是这不会发布它而是让动作在控制器中调用。如何修复
答案 0 :(得分:1)
您可以使用可选参数功能对这两个操作使用一个操作,无需执行其他操作:
public ActionResult Mobiles(int id=0)
{
// id will be initialized 0 when no id passed to action
if(id > 0)
{
// if id is coming do something here
var q = from n in db.Mobiles
where n.ProductId == id
select n;
return View(q);
}
else
{
var q = from n in db.Mobiles
select n; // id is not passed
return View(q);
}
}