简体   繁体   中英

How to pass a parameter value from view to controller?

I would like to pass a parameter value from view to my controller but it is not working. Here is the method which I am using:

@Html.ActionLink("English","ResourseLang","mvcResourseController", new { lang = "value" })

In the controller the lang value is null

public ActionResult ResourseLang(String lang) {
    String ab = lang;
    return view("Index");
}

You will get like below:

@Html.ActionLink("English","ResourseLang", new { @lang = "value" })

At controller side code will be same as you wrote.

public ActionResult ResourseLang(String lang) {
    String ab = lang;
    return view("Index");
}

Ref :

https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/ajax-actionlink-and-html-actionlink-in-mvc/

You can test MVC code online here:

https://dotnetfiddle.net/CsMvc

You are using a wrong Html.ActionLink overload and missing html attributes. Pass null if you are not adding any html attributes. So you should use it like below:

Razor:

@Html.ActionLink(
      "English",                //Action Link Text
      "ResourseLang",           //Action Name
      "mvcResourseController",  //Controller Name
      new { lang = "value" },   //Route Values
      null                      //Html Attributes
)

Controller:

public ActionResult ResourseLang(string lang) {
    string ab = lang;
    return view("Index");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM