Ghazanfar Khan
Ghazanfar Khan

Reputation: 3718

Pass id value from view which is receiving IEnemerable List

I want to pass id value from my view . I am using Html.ActionLink attribute. I am performing like this

  @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>
}

but this doesnot posting it instead get action is calling in controller.How to fix that

Upvotes: 0

Views: 180

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You can use the one action for both things using optional parameter feature, no need of making another action:

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);
}


}

Upvotes: 1

Related Questions