jquery - dynamically fill fields with json based on property name - asp.net

asp.net mvc model object is being fetched by ajax call - $.ajax(....
form has fields with IDs exactly to matching properties on returned json object (created by Html.TextBox("NAME", Model.Order.NAME) )
How to automatically populate fields(inputs) with corresponding json object properties ?
Manually would be like $("#NAME).val(json.NAME) so how to make this dynamic?
Is there some kind of reflections (like System.Reflection in c#) for javascript/jquery ?

Maybe something like this:
$("#formId input").each(function(){
$(this).val(json[$(this).attr("id")]);
});
... which iterates over all the form inputs, and looks for a JSON entry with the inputs ID.
The thing to note here is that you can retrieve json.NAME via json["NAME"].

Related

Access DevExpress JavaScript objects for nested fields

When I render DevExpress MVC controls for nested fields, using a custom FormBuilder class of my own, I'm using the naming convention of ModelName.FieldName. This fixes binding issues I was having, but I now need client-side access to the JavaScript objects that are created for the control.
In the past, I would find an object called FieldName. For nested fields, what is the naming pattern for the JavaScript object name (since ModelName.FieldName would not be a suitable JavaScript object name), and is there perhaps an alternative way to access the object by passing in the full field name as a parameter to some JavaScript method (something like AllControls.GetControl('ModelName.FieldName'))?
Here is a sample of my code:
var textBoxSettings = new TextBoxSettings
{
Name = "ModelName.FieldName",
};
writer.Write(devExpress.TextBox(textBoxSettings).GetHtml());
UPDATE:
It appears that DevExpress does indeed render objects with the name ModelName.FieldName. I'm not sure how to access those objects in JavaScript - is there some kind of escape sequence to treat the entire phrase as a single variable name?
From my understanding the 'DevExpress' way to access controls dynamically is to use the control collection functions
var nameTextBox =
ASPxClientControl.GetControlCollection().GetByName('ModelName.FieldName')
DevExpress does actually create the JavaScript object with a variable name in the form ModelName.FieldName.
To access that object in JavaScript, you can use the format:
var myControl = window['ModelName.FieldName'];

Why is ViewBag called ViewBag?

In asp.net mvc, why is ViewBag called ViewBag?
I'm looking for the history or reason why it's called ViewBag over some other name.
ViewBag is a dynamic mapping of the ViewData dictionary. It's called a "bag" because there's no order or sequence to it.. it's just a bunch of data accessible from a dynamic property, much like if you had a bag of stuff.
The underlying ViewData has order to it, but when it's mapped to the dynamic collection it loses that order.. thus it's a bag.
See a definition here:
http://www.cs.miami.edu/~geoff/Courses/MTH517-00S/Content/ArrayBasedADTs/BagsStacksQueues.html
Its a bag full of information which is made available to the view.
It enables you to dynamically share values from the controller to the view. It is a dynamic object which means it has no pre-defined properties. You define the properties you want the ViewBag to have by simply adding them to the property. In the view, you retrieve those values by using same name for the property.

ASP.NET MVC Render Model as Hidden fields

Is there a way to render a complete model as hidden fields?
Something like:
#Html.HiddenFor(m => m)
Or do I have to render every single property of my model with HiddenFor?
Edit:
It is a complex wizard (5-10 steps). In the last step I want to store the data in the DB. Perhaps I can serialize the model as JSON to a hidden field. Then I could also access it via JS.
Well you could serialize the object to a string of some format, such as XML or Base64, and put that into a hidden field, but that is basically just old-school ASP.NET ViewState.
If you are only editing a couple of fields in a large object, it is generally better to just have the ID on the page (in a hidden field or the URL) and reconstruct it again from the database/session/wherever when they submit the form.

getParameternames() of servlet

In servlets - getParameterNames() returns the name passed in the request.
I want to ask that if we have 3 form tags into one html file and we want to get the names from form1 and form3 then how we can pass the reference of the form with getParameterNames.
Enumeration enum = req.getParameterNames();
Thanks..
You will only get the parameters for the form that is submitted. I'm not sure what you are trying to do, but you could have hidden form inputs that contain the values of the non-submitted forms. You can set those hidden values using javascript.

Accessing ASP.NET Server variables in jQuery

In Page_Load() function, I have populated an arraylist and a hashtable. I need to read their values in the $(document).ready(function(){...}.
I am a beginner in terms of ASP.NET and jQuery. Please suggest a way to do it.
cheers
you can use PageMethods to make a call to your aspx page from JS. Here is a nice article on how to use page method with jQuery. You can return your "whatever" in your page method. If you want to serialize your server side objects before passing them to the client side, you can serialize them to JSON . Here is an example. You would just read your json data after your ajax call is successful("success: function(msg){//do something}") and update your HTML accordingly
you can use Page.ClientScript property to register arrays, scripts etc.
For more information view MSDN
you can create a page.method in server side and call it in the client side script that return an array of values
by adding
<scriptManager EnablePageMethods="true">
in the server side you create a function that return an array of values
ex: GetArrayResult()
if you intersting in this i will give you the rest of example
JSON.Net will help you serialize the hashtable and array to JSON, and you can store these in a hidden field. Then on the client side you can deserialize the strings to your javascript variables with:
var array = JSON.parse($('#hiddenTextField').val());

Resources