Pass a string from HTML Actionlink to Controller for next View

121 views Asked by At

The project works like this. You click on a Project name and it takes you to a Chart. My Chart is configured to receive the string value (ie. Project Name that is clicked on) What do I need to do to make the following work:

My FirstView that will send the string parameter:

@projectOverviewa.DisplayProject_Name_1     //This gets my project's name from the DB table
@Html.ActionLink("View", "ChartForProject", new { id = item.rowid } //here it should get the name of the project clicked on. (For the code above)

The Controller:

public ActionResult SecondViewForChart()
{
      string getName = Session["projectname"].ToString();  //AThe  ActionLink should send the string value to here for me to use in the CHhart.

      return View();
 }

Not sure about using the Session though.

2

There are 2 answers

0
Wouter de Kort On

You are sending the parameter (item.rowid) as a part of query string (part of the url). The easiest way to get it is to use the model binding that ASP.NET has and change your action method to:

public ActionResult SecondViewForChart(int id) // Assuming your id is an int. Otherwise change it to the type of `rowid`
0
sami ullah On
  1. Your First View
@Html.ActionLink("SecondViewForChart", "ChartForProject", new { id = item.rowid,projectName = "Project you want to send" }
  1. Controller Part
   public ActionResult SecondViewForChart(int id, string projectName) 
{
      ViewBag.projectName = projectName; 
  // using ViewBag is better then Session as you just need this string in next View

      return View();
 }
  1. use this where you want to show your project name
@ViewBag.projectName