Click here to Skip to main content
14,494,314 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi All,
I have below code which loads a grid and one of the column in row is clickable on which it loads another page. I want to pass the value of this clicked link in to next page. I dont want it to be through query string. I have done below using query string, Any help please using any other way

What I have tried:

---Html -----
@foreach (DataColumn col in Model.ModelPageDataRow.Columns)
{
@Html.ActionLink(row[col.ColumnName].ToString(), "ActionName", new { PassedId = row[col.ColumnName] })
}

--controller ---

public ActionResult ActionName()
{
Session["SomeId"] = Request.QueryString["PassedId"];
---Rest of the code
}
Posted
Updated 26-Mar-20 1:45am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try TempData

TempData in ASP.NET MVC[^]
   
Comments
Member 14534451 26-Mar-20 12:48pm
   
I dont think tempdata works in my scenario
Rate this:
Please Sign up or sign in to vote.

Solution 2

To pass values from the client to the server, you have two options: make a GET request and pass the value in the query-string; or use a <form> to make a POST request.
@foreach (DataColumn col in Model.ModelPageDataRow.Columns)
{
    using (Html.BeginForm("ActionName"))
    {
        @Html.Hidden("PassedId", row[col.ColumnName]);
        <button type="submit">@row[col.ColumnName]</button>
    }
}
You can use CSS to style the button to look like a link if required.

NB: Forms cannot be nested. If you already have a form wrapping this code, you will have to use the query-string.

On the server side, rather than accessing the Request.QueryString or Request.Form collections, just use model binding to retrieve the value:
public ActionResult ActionName(string passedId)
{
    Session["SomeId"] = passedId;
    ---Rest of the code
} 


Edit: An alternative approach if you already have a form wrapping the code:

Make the buttons submit the parent form with a specific name and value:
@foreach (DataColumn col in Model.ModelPageDataRow.Columns)
{
    <button type="submit" name="SendThisIdToTheOtherAction" value="@row[col.ColumnName]" formnovalidate>@row[col.ColumnName]</button>
}
Update the existing action to check whether one of the buttons has been clicked. If it has, store the value in TempData and redirect to the new action:
public ActionResult ActionThatYourFormPostsTo(string sendThisIdToTheOtherAction = null)
{
    if (!string.IsNullOrEmpty(sendThisIdToTheOtherAction))
    {
        TempData["PassedId"] = sendThisIdToTheOtherAction;
        return RedirectToAction("ActionName");
    }
    
    ...
}
Update the new action to read the value from TempData:
public ActionResult ActionName()
{
    string passedId = (TempData.TryGetValue("PassedId", out object id) ? Convert.ToString(id) : null;
    ...
}
NB: The formnovalidate attribute on the button should prevent the jQuery validation plugin from attempting to validate the form when one of these buttons is clicked. If you've written your own validation scripts, you will need to update them accordingly.
   
v2
Comments
Member 14534451 26-Mar-20 12:50pm
   
I already have a form, is there any other way I can achieve this?
Richard Deeming 26-Mar-20 13:00pm
   
As I said, forms can't be nested.

Your only other option would be to modify the action which your current form posts to so that you can post the value to that, and then use TempData to store it while you redirect to the other action:
@foreach (DataColumn col in Model.ModelPageDataRow.Columns)
{
    <button type="submit" name="SendThisIdToTheOtherAction" value="@row[col.ColumnName]">@row[col.ColumnName]</button>
}
public ActionResult ActionThatYourFormPostsTo(string sendThisIdToTheOtherAction = null)
{
    if (!string.IsNullOrEmpty(sendThisIdToTheOtherAction))
    {
        TempData["PassedId"] = sendThisIdToTheOtherAction;
        return RedirectToAction("ActionName");
    }
    
    ...
}

public ActionResult ActionName()
{
    string passedId = (TempData.TryGetValue("PassedId", out object id) ? Convert.ToString(id) : null;
    ...
}
NB: If you have any validation on your form, it could prevent you from clicking the button. If you're using the jQuery unobtrusive validation, you might be able to work around this by adding formnovalidate="formnovalidate" to the buttons.
Member 14534451 27-Mar-20 5:30am
   
Thank you. This solution worked :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100