i was putting my scripts in my view files (cshtml) when i started working with asp mvc3. i was doing it like this: at the bottom of each cshtml files, i put my scripts, .... and some of those scripts use TempData, everything works fine.
but then somebody told me that's its a bad habit to let users see how i code my scripts. they said that i should create a js file and put all my scripts in there and i should just call them like this:
<script src="@Url.Content("~/Scripts/myOwnLocalScripts.js")" type="text/javascript"></script>.
so i did that but it seems that my scripts now cant read TempData and even session cookies. i have a line in my script like this:
var currentEmployee = '@System.Web.HttpContext.Current.Session["UserNumber"]';
$('.textBoxEmployeeNumber input[id="EmployeeId"]').val(currentEmployee);
it's supposed to display a user number but it is displaying "System.Web.HttpContext.Current.Session["UserNumber"]" as a string....
moreover, i have a code in my script like this:
if ('@TempData["successMessage"]' != "" || '@TempData["successMessage"]' != null) {
function newAlert(type, message) {
$("#alert-area").append($("<div class='alert alert-success " + type + " fade in' data-alert><p><b> " + message + " </b></p></div>"));
$(".alert-success").delay(4000).fadeOut("slow", function () { $(this).remove(); });
}
newAlert('success', '@TempData["successMessage"]');
}
it's supposed to be displayed only when Tempdata[successMessage] contains a value but it keeps displaying everytime the page loads and it is displaying '@TempData["successMessage"]' as a string...
what could be my errors?