How to create a hidden field in MVC 3 | Coding Cluster - using, c#, mvc 4, iphone, php, ios, javascript, in mvc 3 & more

How to create a hidden field in MVC 3


ASP.NET MVC 3: hidden  field in cshtml:
                                       From your model you can creates from your modela hidden input on the form for the field  that you pass it.

It is useful for fields in your Model/ViewModel that you need to persist on the page and have passed back when another call is made but shouldn't be seen by the user.

Consider the following ViewModel class:

public class ViewModeStudent
    public string Value { get; set; }
    public int Id { get; set; }

Now you want the edit page to store the ID but have it not be seen:

<% using(Html.BeginForm() { %>
    <%= Html.HiddenFor(model.Id) %><br />
    <%= Html.TextBoxFor(model.Value) %>
<% } %>
Which results in the equivalent of the following HTML:
<form name="form1">
    <input type="hidden" name="Id">2</input>
    <input type="text" name="Value" value="Some Text" />
Please share this post with your friends. If it's useful to you. Thanks!.


Post a Comment

Share your thoughts here...




Blogger Widgets