Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 ..to maintain data from get method to view and view to post in mvc3 Razor

Thanks in advance
Rangababu
Posted
Updated 11-Feb-13 20:22pm
v2

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.
HTML
<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
 
Share this answer
 
Comments
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 VM 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 ..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 VM 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 ..
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:

$(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);
}
 
Share this answer
 
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...
 
Share this answer
 
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.
 
Share this answer
 
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900