1

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

Satpal
  • 132,252
  • 13
  • 159
  • 168
Toubi
  • 2,469
  • 10
  • 33
  • 49
  • 1
    You cannot use `Request.Form` because you are **not** submitting your form in the code you posted above. You are actually giving your users a `` tag associated with each row that you produce. Clicking on that link shall `not-submit` the form but `re-directs` the page. – now he who must not be named. Oct 21 '13 at 04:44

2 Answers2

2

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 .

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • Passing &form=post in the Url causes such a strange side effect? I don't believe. Could you give a jsfiddle link to the resulting html to prove the concept? – Zruty Oct 21 '13 at 05:43
1

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.

Zruty
  • 8,377
  • 1
  • 25
  • 31
  • 1
    If view has lot of hiddenfield and lot of fields ,Then he need send lot of parameters to a action method ? Is it correct ? – Ramesh Rajendran Oct 21 '13 at 04:59
  • In this case it's better to convert the hyperlink into a submit button, like I also suggested. – Zruty Oct 21 '13 at 05:01
  • Can he? Can a form have multiple **submit** buttons ? – now he who must not be named. Oct 21 '13 at 05:02
  • @Zruty, thanks. As I explained in my question I cant include this field in Query string. Can you please guide how I can convert it into a form/ submit button, the other workaround you mentioned ? – Toubi Oct 21 '13 at 05:04
  • 1
    But if he does not like to button , if he want to only link button , What we do ? send too many parameters ? – Ramesh Rajendran Oct 21 '13 at 05:05
  • A form can indeed have multiple submit buttons, but in this case it has to be multiple forms. – Zruty Oct 21 '13 at 05:06
  • @Zruty, will i need to place form tag as well in my markup ? Wil it have multiple form tags, or form tag inside loop? I am sorry I dont have its idea. Can you please clear ? – Toubi Oct 21 '13 at 05:19
  • The `@Html.BeginForm()` part will generate a `
    ` tag. You will have one form per table row.
    – Zruty Oct 21 '13 at 05:21
  • @Zruty Finally i found it for my doubt, if we add form="post" in the anchor tag,The OP code was working and he don't need send too large parameters , and submit button .. be coool – Ramesh Rajendran Oct 21 '13 at 05:22