Thursday, 14 April 2016

Access Hidden Or HiddenFor Fields Value At Controller End In Asp.Net Mvc Using C#

4/14/2016 - By Pranav Singh 0

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.

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.





About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top