Click here to Skip to main content
13,196,126 members (48,819 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Alternative for Hidden fields in mvc razor
In MY project ..

1. I created a model with 10 properties
2.i am using that model in get action and assign those 10 properties with database values.
3.And pass those values to view through the model..
4.And in view i displayed only 2 fields with editable and remain 8 fields are not used in view.
5.but when i was press submit button i want pass 2 and remain 8 fields ..
6.actually i am trying for this but in post method i get null to the remain 8 fields ..
7.We can maintain 8 fields in view using @html.HiddenFor()..
8.but my requirement is without using hidden fields,session and cache maintain data from get method to view and view to post in mvc3 Razor

Thanks in advance
Posted 11-Feb-13 3:07am
Updated 11-Feb-13 20:22pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You should set the values from the database to any of the input controls.If you don't want to set in hidden field use like below.

Create a div and add a style display:none to the div and place the remaining fields inside the div and set all the values in textBoxes.
<div style="display:none">
//Set your remaining input field here

I think there is no problem of using hiddenfield because your are using post method.It is a secured method.

Hope this helps
rangababu123456 12-Feb-13 0:51am
i think display:none internally same as hidden fields..hiddenfields concept is better for no.of fields are 10 or less than 10..but i have hundred fields at that time i think hidden 100 fields are not good way ..
Jameel Moideen 12-Feb-13 4:38am
No. Display none hide the div only.
rangababu123456 13-Feb-13 0:40am
if we put display none to div also act as hidding ...from ur aspect instead hidding fields directly put fields in div (in directly as same as hidding)..any other than the display none of div
Jameel Moideen 13-Feb-13 3:55am
you should set text in textBoxes(not hiddenfield) inside the div
rangababu123456 14-Feb-13 1:04am
in side div and hidden there is not much difference ..don't hide or div display none to fields in view ..i want alternative way means i am not holding the in form div or display none ..
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

The best way to my mind is to use JQuery ajax call to send parameters to controller.
This way you need to bind your control with some extra attributes which are your properties to post on Controller.

First of all in your view instead of hidden field you can add attribute to existing controller like:

Now in JavaScript section put on Document ready portion like:

// Write handler to bind function to submit button Click in View


// First of all Get the Submit button which was clicked
var submit = $(this);

// Now you're able to read all binded attributes to this button one by one through its properties

var property1 = submit.attr('data-YourProperty1');
var property2 = submit.attr('data-YourProperty2');

// and then Parameter array

var parameter = { Property1: property1, Property2: property2};

url: '@Url.Action("Your Action", "Your Controller")',
data: parameter,
type: 'POST',
async: true,
cache: false


Now in Controller:

public ActionResult YourAction(string Property1, string Property2)
/// Your logic
return Json(true, JsonRequestBehavior.AllowGet);
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

with the help of html.actionlink you have to pass the data from view to controller

@Html.ActionLink("Pass Data","YourActionName","YourControllerName",new {data1=data1,data2=data2,data3=data3},null,null)

and in your actionResult you have to pass these data parameter like

public ActionResult YourActionName(string data1,string data2,string data3)
return view();

here in action you get your data from view to controller.

Hope this help...
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

I have faced this same problem while editing one set or record, there only 2 fields are to be updated but for rest of the fields i was faced the same.

My alternate way was - get the ID -> fetch data from table by that ID -> update that 2 fields -> save the object.
Rate this: bad
Please Sign up or sign in to vote.

Solution 6

Hi RangaBabu,
I too faced the same issue in my project,

I have used ajax call to update the fields on to the database for that I used serializeobject to convert model to object before that I enabled all the controls using javascript and after serializing again I disabled all the controls.
By doing this you will get all 10 values to the object once initialized disable the controls that you do not want to.

Hope this helps you a bit.

If you have any doubt feel free to ask.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171019.1 | Last Updated 13 Nov 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100