Html.ActionLink does not go through value

I am trying to pass a value from my view to my controller based on what I click using HTML.ActionLink. For example, if I click on Soccer, I want the value of Soccer to be passed to the controller, after which it changes the display. Currently, when I click on the name of the sport, nothing happens, and when I debug the project, the parameter of the name of the sport in the controller is NULL, which means that this value was not passed to the controller.

This is the code from the view:

         @foreach (var item in Model.Sports)
    {
        <p>
            @*@Html.ActionLink(@item, "Index", "Home")*@
            @Html.ActionLink(@item, "Index", "Home")

        </p> 
    }


</div>
<div class="col-md-4">
@foreach (var Coupon in Model.CurrentCoupons())
{
    <p>
        Coupon: @Coupon.CouponName <br />
        Event Count: @Coupon.EventsCollection.Count
        <br />
        Event:
        @foreach (var Event in Coupon.EventsCollection)
        {
            @Event.Name;<br /><br />
        }

        @foreach (var Market in Coupon.MarketList)
        {
            @Html.ActionLink(@Market.Name, "Index", "Home")<br />
        }
    </p>

    <table>    
        @foreach (var Selection in Coupon.SelectionList)
        {
            <tr>
                <td width="400">@Selection.Name</td>
                <td>@Selection.PriceFixed</td>
            </tr>
        }        
    </table>
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index(string sportName)
    {
        ViewBag.Title = sportName;
        return View(new TestDataHelper(sportName));
    }
}
+4
source share
3 answers

@Html.ActionLink routevalues, , - :

@Html.ActionLink("Soccer", "Index", "Home", new {sportName = @item}, null)
+3

:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

id:

public ActionResult Index(string id)
{
    ViewBag.Title = id;
    return View(new TestDataHelper(id));
}
+2

you can pass the parameter to the controller using an anonymous type, for example:

@Html.ActionLink(@item, "Index", "Home", new { sportName = item })

check out the examples here: http://www.w3schools.com/aspnet/mvc_htmlhelpers.asp

+2
source

Source: https://habr.com/ru/post/1544483/


All Articles