Viewdata in jquery syntax - javascript

I am working on an asp.net MVC application. I want to access Viewdata and assign it to Javascript variable
var LeaveDays = #(Html.Raw(ViewData["LeaveDays"]));
but it says syntax error at semi colon ; What is syntax. If i remove semi colon, it says statement not terminated. In both cases because of Js error, other scripts are not run on button click or page refresh
I want to assign the value to var
Please suggest.

Pass you value in the quotes.
Use
var LeaveDays = '#(Html.Raw(ViewData["LeaveDays"]))';

try this....
if want to assign to var :
# { var LeaveDays = ViewData["LeaveDays"]; }
or to display :
#Html.Raw(ViewData["LeaveDays"])

Related

Add child to E4X with my variable

This is using Mirth Connect which uses E4x and js.
Basically I have a variable that I want to populate the XML with.
var memberid = "1234";
var fieldsxml = new XML(<fieldvaluelist></fieldvaluelist>);
fieldsxml.field += <fieldvalue templatefieldid="446" value=#memberid/> //memberID
But its giving an error on the 3rd line: (I also tried just memberid without quotes)
DETAILS: TypeError: Open quote is expected for attribute "value"
associated with an element type "fieldvalue".
It works if the third line is this:
fieldsxml.field += <fieldvalue templatefieldid="446" value="memberid"/>
But that just adds the literal string "memberid" . I actually want value="1234" instead.
How can I do this?
Edit: The final XML should look like this.
<fieldvaluelist><fieldvalue templatefieldid="446" value="1234"/></fieldvaluelist>
You're almost there. Instead of using #memberId, use {memberId}:
fieldsxml.field += <fieldvalue templatefieldid="446" value={memberid}/>;

Init jQuery function and pass a variable with a string

Hardcoded i would initilize (a plugin in this case) like so, which is working:
var cal = $("#calendar").calendario({
caldata : {
'09-11-2015_1':['09-11-2015',0,19]
}
});
Now i want to pass the caldata option a variable with the content like that:
var init_events = $("#init_events").val();
var cal = $("#calendar").calendario({
caldata : init_events
});
init_events has the value {'09-11-2015_1':['09-11-2015',0,19]}
But that doesnt work. If I log the output of the option inside the plugin it just returns a string in the console where as if I log the first Code it logs an Object.
I tried jQuery.parseJSON(init_events) but this returns an Unexpected token error.
Any idea how i could get this working with passing a variable?
init_events is not valid JSON. JSON only allows double quotes around strings, not single quotes, so it should be:
{"09-11-2015_1":["09-11-2015",0,19]}

SYNTAX HELP: How to Find control in javascript ?

I am trying to implement server side code on client side. "trvddl1" is a ascx control which contains a dropdownlist "ddltree". It was easy on server side but I am facing difficulty using the same in javascript.
How do I write the following code in javascript?
((DropDownList)trvddl1.FindControl("ddltree")).SelectedValue;
I tried
var abc = document.getElementById('<%=trvddl1.ClientID%>').value;
and
var Region = document.getElementById('<%=trvddl1.FindControl("ddltree")%>').value;
but javascript returned error. Is there some other keyword I am missing ?
Check the HTML output (Browser-->View Source) and locate the control there, see what the ID of that control has, and put that one into the getElementById() function.
Example:
<input id='ddltree' .... />
Then use:
var abc = document.getElementById('ddltree').value;
Perhaps you can try something like that:
// find all controls that have an id that ends with ddltree
// starts with would be [id*=ddltree]
var abc = document.querySelectorAll("[id$=ddltree]");
if(abc.length > 0) {
// got it !
console.log(abc[0].value);
}
Please note that querySelectorAll is not supported in all browsers (even though - most). Here is a reference.

pushing json arrays in javascript

I am customizing some jQuery plugin, and I have an error message I can't understand
var totHistory=0;
var positions = new Array();
$('.someclass').each(function(index){
var tmp = $(this).val();
addHistory({id:tmp});
});
function addHistory(obj)
{
/* Gets called on page load for each comment, and on comment submit */
totHistory++;
positions.push(obj.id);
}
At the very first iteration through .someClass, I get this message
Cannot call method 'push' of undefined
Could someone explain why ?
You should either send positions as a parameter or declare it in a scope accesible for addHistory. You should not declare it without the var keyword as that is considered a bad practice.
Try my first suggestion as that one is the only one I can help you with without knowing the structure of your other js code.

how to define dynamic variable in javascript

when i define in javascript
var whoami = #ViewBag.myname
it is not work or render they render
var whoami = ;
i am trying it #(ViewBag.myname) // not worked too.
are their any way to do this in raor MVC 3
Is #ViewBag.myname empty?
Enclose the variable in quotes, so to have a correct javascript string:
var whoami = "#ViewBag.myname";
also ensure that myname doesn't contain quotes too (or escape them).

Categories

Resources