reading hidden field value in action?

2019-08-15 04:17发布

IN MVC-4 Razor view I have a html table with 2 columns. 1st columns shows a field and also stores it as a hidden field. 2nd column of table has a <a> clicking on it it redirect to some other control and action.

<tbody>
    @foreach (var row in Model.Conversions)
    {
     <tr>
    <td>@(Html.DisplayFor(m => row.LastUpdatedDate))
         @(Html.Hidden("DateFrom",@row .LastUpdatedDate))
    </td>
    <td><a href="@Url.Action("ExchangeRateDetails", "ExchangeRate", new { currencyCode = @row.CurrencyCodeFromTo })">+</a>
    </td>
    </tr>
    }
</tbody>

Inside action I am trying to read hidden field value but it is null. Here is my code:

public virtual ActionResult ExchangeRateDetails(string currencyCode)
        {

            var dat = Request.Form["DateFrom"];

}

Problem:

I am finding hidden field value as null. My expectation is it should have value coming form hidden field. I cant pass this value as a query string. Can you please guide and help me how I can read hidden field values in action ?

Much thanks for your guidance and helping me.

Thanks

2条回答
Evening l夕情丶
2楼-- · 2019-08-15 04:34

Html.Hidden(...) generates a hidden <input>. The <input> values are only sent to the server when you submit a form (by clicking a <input type="submit"/>).

In your case, you're creating a hyperlink to another action. When the user clicks it, he will go to the specified URL and he will NOT submit the current form.

You should either turn your hyperlink into a submit button and make it submit a form to the action you want, or (easier to do in your sample) to include the field value into the URL:

<td><a href="@Url.Action("ExchangeRateDetails", "ExchangeRate", new { currencyCode = row.CurrencyCodeFromTo, DateFrom = row.LastUpdateDate })">+</a>

EDIT: Here's a solution with forms instead of hyperlinks:

<tbody>
    @foreach (var row in Model.Conversions)
    {
     <tr>
        @using (Html.BeginForm("ExchangeRateDetails", "ExchangeRate"))
        {
            <td>@Html.DisplayFor(m => row.LastUpdatedDate)
                @Html.Hidden("DateFrom", row.LastUpdatedDate)
                @Html.Hidden("currencyCode", row.CurrencyCodeFromTo)
            </td>
            <td>
                <input type="submit" value="+" />
            </td>
        }
    </tr>
    }
</tbody>

If you want to keep hyperlinks and not buttons, you can do this too, but you'll have to write some JavaScript to submit a form when a user clicks a link. Alternatively, you can re-style your submit button to look like a hyperlink.

查看更多
SAY GOODBYE
3楼-- · 2019-08-15 04:43

Use FormCollection

[HttpPost]
public virtual ActionResult ExchangeRateDetails(FormCollection collection,string currencyCode)
{
     string value = Convert.ToString(collection["DateFrom"]);
     ...
     return View();
}   

and Why you used @( then hidenfield ? please change that like , remove '()' in before html.hidden

 @Html.Hidden("DateFrom",@row .LastUpdatedDate)

EDIT

And Add form="Post" in your anchor tag

like

<a href="@Url.Action("ExchangeRateDetails", "ExchangeRate", new { currencyCode = @row.CurrencyCodeFromTo **,form="post"**})">+</a>

It's working to me . If you add form="Post" in your anchor tag by the above edited code , Then you don't need any other changes ,Your code always working with on this small change .

查看更多
登录 后发表回答