In this article I will show you how you can access the
hidden field and hiddenfieldfor value using c#.net in asp.net mvc. This article we can use in MVC2, MVC4, MVC3, MVC4, MVC5, MVC6.
Some of my previous articles are as follows: Validation
Of Form To Display Error Message as ToolTip Using jQuery In Asp.Net MVC,
Twitter,
Pinterest, Facebook, GooglePlus and Tumblr Social Sharing Buttons Using jQuery
In Asp.Net MVC, Fixed
Header Web Page Using CSS3 Without jQuery In Asp.Net MVC, CountDown
To Show Under Construction Page Using jQuery In Asp.Net MVC, Bootstrap
Style Dynamic jQuery Dropdowns Menu Using Asp.Net MVC In C#.Net, Blink
Title or Multiple Browser Title Using jQuery in Asp.Net, MVC, Social
Share Buttons of Facebook, Twitter, Google Plus and Pinterest Using jQuery in
Asp.Net MVC.
So for this article first we will create an asp.net mvc
application and add a model class file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication2.Models
{
public class HiddenFieldValueModel
{
public
string HiddenFieldValue { get; set; }
}
}
|
In this we will add a controller file and add the below
code.
public ActionResult
Index()
{
HiddenFieldValueModel
hiddenFieldValueModel = new HiddenFieldValueModel();
hiddenFieldValueModel.HiddenFieldValue
= "This is HiddenFieldFor Value";
return
View(hiddenFieldValueModel);
}
|
In above code I have passed the model value to bind it to
hidden field. Now create the view and add the below code.
@model MvcApplication2.Models.HiddenFieldValueModel
@{
ViewBag.Title = "Access
Hidden Or HiddenFor Fields Value At Controller End In Asp.Net Mvc Using
C#";
}
@using (Html.BeginForm("Index", "Home"))
{
@Html.Hidden("hiddenValue",
"This is HiddenField Value")
@Html.HiddenFor(m => m.HiddenFieldValue)
<div>
HiddenField Value:@ViewBag.HiddenField</div>
<div>
HiddenFieldFor Value :@ViewBag.HiddenFieldFor</div>
<input type="submit" value="Submit" />
}
|
In above code I have taken a hidden field and hidden field
for. Now we will add the post code.
[HttpPost]
public
ActionResult Index(HiddenFieldValueModel hiddenFieldValueModel, string hiddenValue)
{
//Way 1
To get value
ViewBag.HiddenField =
hiddenValue;//Get HiddenField Value
ViewBag.HiddenFieldFor =
hiddenFieldValueModel.HiddenFieldValue;//Get
hiddenFieldFor Value By Model
//Way 2
Te get value
string
value1 = Request.Form["hiddenValue"].ToString();
string
value2 = Request.Form["HiddenFieldValue"].ToString();
return
View(hiddenFieldValueModel);
}
|
In above code I have shown two ways to access the hidden
field value and for value on postback. Have a look the code output for way 1.
Way 2 output:
Now we have done run the application to check the output.
0 comments:
Please let me know your view