Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC MVC3 Razor hidden field
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 ..to maintain data from get method to view and view to post in mvc3 Razor
 
Thanks in advance
Rangababu
Posted 11-Feb-13 4:07am
Edited 11-Feb-13 21:22pm
v2
Rate this: bad
good
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
</div>
 
I think there is no problem of using hiddenfield because your are using post method.It is a secured method.
 
Hope this helps
  Permalink  
Comments
rangababu123456 at 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 at 12-Feb-13 4:38am
   
No. Display none hide the div only.
rangababu123456 at 13-Feb-13 0:40am
   
if we put display none to div ..in 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 at 13-Feb-13 3:55am
   
you should set text in textBoxes(not hiddenfield) inside the div
rangababu123456 at 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
good
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:
 
<input class="btnSubmit" type="Button" data-yourproperty1="@Model.Property1" data-yourproperty2="@Model.Property2" />
 

Now in JavaScript section put on Document ready portion like:
 
$(document).ready(function(){
// Write handler to bind function to submit button Click in View
 
$('.btnSubmit').click(function(){

// 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};
 
$.ajax({
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);
}
  Permalink  
Rate this: bad
good
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
Example
 
@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...
  Permalink  
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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.
 
Regards,
RK
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 120
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 13 Nov 2013
Copyright © CodeProject, 1999-2014
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