mothee
mothee

Reputation: 141

How to set a value for MVC2 Html.HiddenFor from ViewData

I tried below method, but it does not work:

<%: Html.HiddenFor(m => m.Email, (string)ViewData["Email"])%>

<%: Html.HiddenFor(m => m.Email, new{value = (string)ViewData["Email"]})%>

Plz adivce.

Upvotes: 0

Views: 3641

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039458

In ASP.NET MVC data is set by the controller. A view is there only to display the data passed by a controller. Also I would recommend you using strongly typed views and view models instead of ViewData.

public ActionResult Index()
{
    var model = new SomeViewModel
    {
        Email = "[email protected]"
    };
    return View(model);
}

And in the view simply:

<%= Html.HiddenFor(m => m.Email) %>

Upvotes: 2

Related Questions