MVC Condition Compilation Symbols Debug - Release - asp.net-mvc-3

It is my understanding that under the Project->Properties->Build settings there is a 'Define DEBUG constant'. By default the "Debug" configuration has this option checked, which means that '#if DEBUG' should evaluate to try. Also by default the "Release" configuration has this option not checked.
I am programming under vs2010 sp1 in a MVC 3 application and the following is what i have done:
#{
#if DEBUG
<script language="javascript" type="text/javascript">
$(document).ready(function () {
// put all your jQuery goodness in here.
alert('Debug Build');
});
</script>
#else
<script language="javascript" type="text/javascript">
$(document).ready(function () {
// put all your jQuery goodness in here.
alert('Release Build');
});
</script>
#endif }
My problem is that regardless of the build type, Release or Debug, i am getting the alert for 'Debug Build'.
What am i doing wrong?

#{
if(System.Diagnostics.Debugger.IsAttached)
{
<script type="text/javascript">
</script>
}
}
Will work, but isn't optimized like #DEBUG and will be hit even if being debuged outside VS

That will never work unfortunately. You will need to put something in the viewbag that you set from your controller.

Related

Why does the browser tell me the parse is not defined even if I have imported it in the head of the HTML file?

I'm trying to connect my HTML files with the parser server. I followed the direction of the back4app guides and added the following code to the head of index.html. But the browser kept telling me Parse is not defined.
<script src="https://npmcdn.com/parse/dist/parse.min.js"></script>
<script type="text/javascript">
Parse.serverURL = "https://parseapi.back4app.com";
Parse.initialize(
"MY_APP_ID",
"MY_JS_KEY"
);
</script>
Can you please test the code below?
<script src="https://cdnjs.cloudflare.com/ajax/libs/parse/2.1.0/parse.js"></script>
<script type="text/javascript">
function myFunction() {
Parse.initialize("APP_ID", "JS_KEY");
Parse.serverURL = 'https://parseapi.back4app.com/';
}
/</script>

TypeError: Joomla.JText._ is not a function

I'm trying to add language translations to my js files. So I've added this code to my view.html.php file in my component:
JText::script('COM_TEST_ENTER_LABEL');
If I look at the html source I now see this:
<script type="text/javascript">
(function() {
var strings = {"COM_TEST_ENTER_LABEL":"Enter a label"};
if (typeof Joomla == 'undefined') {
Joomla = {};
Joomla.JText = strings;
}
else {
Joomla.JText.load(strings);
}
})();
</script>
Now I try to add this to my js file:
alert(Joomla.JText._('COM_TEST_ENTER_LABEL'));
But I just get the error in firebug: TypeError: Joomla.JText._ is not a function
I'm wondering if it has something to do with jQuery. I have a bunch of jquery scripts in the code (view.html.php) that are added after:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.migrate/jquery-migrate-1.1.1.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js" type="text/javascript"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js" type="text/javascript"></script>
<script src="/components/com_test/js/cam.js" type="text/javascript"></script>
<script src="/js/jquery.noconflict.js" type="text/javascript"></script>
If you look at the html source this is before the JText::script stuff. Not sure if that is the cause? If it is I'm not sure what I can do about it? Joomla is ordering this by itself as my code tried to put it first.
Joomla.JText._ is a MooTools based function, so you'll need to allow MooTools to load in your template (if you're removing it, a lot of folks do).
You can access the strings using the following notation code.
alert(Joomla.JText.strings.COM_TEST_ENTER_LABEL);
This looks up the string COM_TEST_ENTER_LABEL in the string property of the JText property of the Joomla object.
Note that JText::_ is a PHP function.
Hope that helps..

Check ViewBag Value is present in javascript

I am using razor template and following is the scenario
$(function(){
//if (ViewBag.IsCallFunction){
somefunction();
//
//do something else
});
If a viewBag variable is present, i.e. not null and if it is set to true, then I would like to call some javascript function. How do I do this?
#{if(ViewBage.somevalue!=null && ViewBage.somevalue=="true")
{
<script type="text/javascript">
somefunction();
</script>
}
}
but remember this will get called as rendered, as per OP, you can't call it, you can render it, so call it inside document.ready when document is loaded
<script type="text/javascript">
$(function() {
#if (ViewData.ContainsKey("IsCallFunction") && ViewBag.IsCallFunction)
{
<text>somefunction();</text>
}
});
</script>
But I would recommend you using a view model instead of ViewBag, because in this case your code could be simplified:
<script type="text/javascript">
$(function() {
#if (Model.IsCallFunction)
{
<text>somefunction();</text>
}
});
</script>
You don't call a JavaScript function from Razor code because Razor runs on the server and JavaScript runs on the client.
Instead, you can emit JavaScript code to the client that then runs once the browser loads the HTML code generated by Razor.
You could do something like
<script type="text/javascript">
#* The following line is Razor code, run on the Server *#
#if (ViewData.ContainsKey("IsCallFunction") && ViewBag.IsCallFunction) {
#* The following lines will be emitted in the generated HTML if the above condition is true *#
$(function(){
somefunction();
//do something else
});
#} #* This is the closing brace for the Razor markup, executed on the Server *#
</script>

Validating form in mvc3 modal dialog window

I am using mvc3 with the KendoUI window control to open up a partial view in a modal window.
I have a form with the popup that I am trying to validate before sending the form back to the server.
I have a click event on my main view that looks like
$("#submit-campaign").live("click",function () {
var form = $("#Send");
$.validator.unobtrusive.parse($(form));
form.validate();
if (form.valid()) {
console.log("valid");
} else {
console.log("invalid");
}
});
However its always been returned as true even if I haven't added values to some of the required.
I have referenced the 3 javascript files like
<script src="#Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
What do I need to do so I am getting the actual validation state clientside from the popup?
The correct way to check validation would be.
$("#submit-campaign").live("click",function () {
var form = $("#Send");
$.validator.unobtrusive.parse($(form));
var val = form.validate();
if (val.valid()) {
console.log("valid");
} else {
console.log("invalid");
}
});

MVC3 date Picker

I am trying to implement an MVC3 DatePicker but having trouble. I have the following code:
<script type="text/javascript">
/// <reference path="jquery-1.5.1.js" />/// <reference path="jquery-ui-1.8.11.js" />
$(document).ready(function () { $('.date').datepicker({dateFormat: "dd/mm/yy"});});
</script>
<div>
#Html.TextBox("SRRDate", Model.SRRDate.ToString(), new { #class = "date" })
Start Date
</div>
However, I am getting an "Microsoft JScript runtime error: Object doesn't support this property or method" in jquery-1.5.1-min.js
Any ideas?
Are you referencing the jQuery script and a jQuery UI script that contains the datepicker plugin? Both script references should appear before your block of code.
The /// <reference path="jquery-1.5.1.js" /> lines are references for providing intellisense for JavaScript and should go in the relevant script i.e. in this case, this should reference the .vsdoc version of the jQuery script and go at the top of the jQuery script file.
In summary, the layout should be
<script type="text/javascript" src="jquery-1.5.1.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.11.js"></script>
<script type="text/javascript">
$(document).ready(function () { $('.date').datepicker({dateFormat: "dd/mm/yy"});});
</script>
#Russ....thanks for the assistance. I found the issue. I had a Telerik scritp Registrar which was included in the _Layout.cshtml. That was causing the error. I imagine the Telerik js files are old. When I look at their documentation, their code is using MVC2 style

Resources