Sarra Sarra - 2 days ago 5
ASP.NET (C#) Question

Pass data from view to controller using asp.net mvc

I want to select a line of references to get the list of athor reference associat ,
this is the code of my view :

<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Réf_OE)
</th>
<th>
@Html.DisplayNameFor(model => model.GENER_MOTORS)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Réf_OE)
</td>
<td>
@Html.DisplayFor(modelItem => item.GENER_MOTORS)
</td>
<td>
@Html.ActionLink("Select", "Select", new { Réf = item.Réf_OE })
</tr>
}




This the code of my select method :

[HttpGet]
public ActionResult Select(string Réf)
{
if (Réf != null)
{
var autres_ref = db.Autre_Références.Where(c => c.Réf_id == Réf).ToList();
ViewBag.autres_ref = autres_ref;
return RedirectToAction("Index", "Référence_Consctructeur");
}
else
{
return RedirectToAction("Create", "Référence_Consctructeur");
}
return RedirectToAction("Index", "Référence_Construscteur");
}


and i put the cursor on select it shows :

http://localhost:56616/R%C3%A9f%C3%A9rence_Consctructeur/Select/7700308756


I didn't understand why the paramter passed to select is null
please help.

Answer

The parameter name you are passing in your ActionLink should match with the parameter name in your action:

@Html.ActionLink("Select", "Select", new { Réf= item.Réf_OE }) 

Or change the parameter name of your action:

 public ActionResult Select(string id){...}
Comments