cannot add javascript file to cshtml - asp.net-mvc

i want to add my customer javascript file to my cshtml
i am working with mvc4
i tried these two ways
#Scripts.Render("~/Scripts/Register.js")
<script type="text/javascript" src="~/Scripts/Register.js"></script>
nothing works.
i want to do this because i want to check for select change.
$(document).ready(function () {
$('#selectRegisterType').on('change', function () {
alert("ddddddd");
});
});
also this is the code of the html
<select id="selectRegisterType">
<option value="None">Select One</option>
<option value="tenant">Tentant</option>
<option value="Apartment Owner">Apartment Owner</option>
</select>
any help would be appreciated

Using MVC4 is really good and you should take advantage of the BundleCollection in your App_Start folder you'll see the BundleConfig.cs and there you can add your javascript and styles and others.
now what you need to write is
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/Scrips/Register").Include(
"~/Scripts/Register.js"));
}
and in your footer tag inside your body tag
</footer>
#Scripts.Render("~/bundles/Register")
#RenderSection("scripts", required: false)
</body>
< /html>
You could otherwise use some script to link directly to your script url like
</footer>
<script type="text/javascript">
var e = document.createElement('script');
e.src = '#Url.Content("~/Scripts/Register.js")';
e.type = 'text/javascript';
document.getElementsByTagName("head")[0].appendChild(e);
</script>
</body>
</html>

WHERE do you put your script? As this looks like JQuery code, do you load JQuery before?
I put your code in JSFiddle, looks like it's working just fine as is:
$(document).ready(function () {
$('#selectRegisterType').on('change', function () {
alert("ddddddd");
});
})
http://jsfiddle.net/h2L6d/
(Duplicated your code here as I can't put a link to JSFiddle without some code)
It seems to me the only reason it would not be "working" is that you are not loading JQuery BEFORE calling your script. Usually, I load those js libraries below the footer of _Layout.cshtml:
</footer>
#Scripts.Render("~/bundles/js")
<script src="~/Scripts/jquery.slidePanel.js"></script>
#RenderSection("scripts", required: false)
</body>
and scripts specific to a page in a script section, at the bottom of the cshtml view:
#section scripts {
#Scripts.Render("~/bundles/jstree")
}
This way I am sure my js libraries are always loaded before my local page scripts. The script which is not in a bundle has been added later on, and should be bundled too.

Related

Adminlte select2 not working for select box in angularjs

AdminLte select2 not working means not showing search in select box, i have included css and js file and used like below and also i have included bootstrap related css and js files
<html>
<head>
<link rel="stylesheet" href="bower_components/select2/dist/css/select2.min.css">
</head>
<body>
<select class="form-control select2" style="width: 100%;" ng-options="t.id as t.name for t in tags" ng-model="t_id">
<option selected="selected">Select Tag</option>
</select>
<script src="bower_components/select2/dist/js/select2.full.min.js"></script>
<script>
$(function () {
//Initialize Select2 Elements
$('select2').select2();
});
</script>
</body>
</html>
It will work
$('.select2').select2();
you may need to load Ajax.googleapis cdn from google. My problem was like as yours.I have faced the problem and solved it. Main problem was select2 doesn't get ajax.googleapis cdn. So, before the jQuery code I have loaded the cdn and it works fine now. You mush notify the version of your cdn. Firstly, the code was
<script>
$(document).ready(function() {
$('.js-example-basic-multiple').select2();
});
</script>
The problem is looked like the picture below
Now after solving the problem the code looks like below
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$('.js-example-basic-multiple').select2();
});
</script>
Now it is seen like below

Scripts are not getting rendered

Cshtml code.
#Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { #class = "datefield", type = "date" } })
#section Scripts {
#Scripts.Render("~/Content/css")
#Scripts.Render("~/Scripts/jquery")
}
Bundle.config code
bundles.Add(new ScriptBundle("~/Scripts/jquery").Include(
"~/Scripts/jqueryui1.12.s",
"~/Scripts/jqueryui-12.js",
"~/Scripts/DateTimePicket.js"
));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/jqueryui1.12.css"
));
code of DateTimePicker
$(function () {
alert("hi");
$(".datefield").datepicker();
});
where is the issue.
The code of these file "~/Scripts/jqueryui1.12.s", "~/Scripts/jqueryui-12.js", are from
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
I am just trying to add the datepicker and the code got messed up.
I think the Issue is you are trying to add css and javascript also please cross the spelling of the DateTimePicker js file
You render css in cshtml you need to use the below line
#Styles.Render("~/Content/css")
Hope this helps!!
if design is going messy then it will definitely shows the errors in browser console.First you should find out which js erros are coming..
and you have placed jquery file ~/Scripts/DateTimePicket.js. Is this "DateTimePicket" or "DateTimePicker" ?? If its spelling mistake then also it can create issue as well..
And if this is not spelling mistake then check with second scenario, you have created section scripts but have u used #RenderSection("scripts", required: false) in layout? Or you can try by adding your renders at the top of the cshtml code as below.
#Styles.Render("~/Content/css")
#Scripts.Render("~/Scripts/jquery")
with Not creating any section,try directly renders.

Change page on swipeleft/swiperight

I have problem with mobile/tablet version of page which I create
I need this: When someone look at product detail and move him to right/left (touch/swipe) then I need load next/previous product detail (= Example: Now I on page with id=2 when I go right, then I load page with id=3, npw I on page with id=3 and I go left then I load page with id=2.. etc.. with different pages id).
I look in thread here, but this solution is not dynamic and I have problem applicate it. Next I inspirated in W3C here.
Now I have these codes which not work:
JS file:
$(document).on('pageinit', function() {
$('.epicFullscreen').bind('swipeleft', function(event,ui)
{
$.mobile.changePage("http://www.some_domain.cz/category/page_name-1/","slide");
});
$('.epicFullscreen').bind('swiperight', function()
{
$.mobile.changePage("http://www.some_domain.cz/category/page_name-3/","slide");
});
});
$(document).on('pagehide', function () { $(this).off('swipeleft swiperight'); });
HTML:
<script src="funkce/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="funkce/jquery.mobile-1.3.2.min.js" type="text/javascript"></script>
<script src="funkce/skripty.js" type="text/javascript"></script>
I need use old JQuery 1.7.1. because this site using it and everything works fine, with newer JQuery I can have new problems. For that I use older JQuery-mobile 1.3.2. which must work with JQuery 1.7.1. ("jQuery Mobile 1.3 supports jQuery 1.7 and newer") . I can´t include "jquery.mobile-1.3.2.min.css" because this styles not work together with my styles correctly.
HTML in body is like that:
<div class="epicFullscreen">
<img class="epicImg" src="../../data_9/11normal.jpg" alt="inspirace č.2" title="inspirace č.2" border="0" />
</div>
Now I have dynamic solution with arrows (right/left) and with adding correct urls with PHP, but on tablet/mobile user like using touch not clicking on arrows etc...
=> Where I have problem and why not work my current solution? And how I can change this static JS to dynamic JS?
try this simple code .....
$(document).on("swipeleft", '#'+event.target.id, function () {
var nextpage = $(this).next('div[data-role="page"]');
if (nextpage.length > 0) {
alert(nextpage.attr('id'));
$.mobile.changePage(nextpage, "slide", false, true);
}
});
$(document).on("swiperight", '#'+event.target.id, function () {
var prevpage = $(this).prev('div[data-role="page"]');
if (prevpage.length > 0) {
$.mobile.changePage(prevpage, { transition: "slide", reverse: true }, true, true);
}
});
I found solution for that I make this answer. I simplify my problem and create simple page.
Code
<!DOCTYPE html>
<html>
<head>
<!--<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">-->
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="jquery.mobile-1.3.2.min.js" type="text/javascript"></script>
<script>
$(document).on("pageinit",function(){
var pathname = $(location).attr('href');
var leva = $('#le').attr("href");
var prava = $('#pr').attr("href");
$("body").on("swiperight",function(){
<!--alert("You swiped right!");-->
location.href=prava;
});
$("body").on("swipeleft",function(){
<!--alert("You swiped left!");-->
location.href=leva;
});
});
</script>
</head>
<body>
<div id="test_div" style="display: block;">
<img src="test.gif" width="100px">
<a id="le" href="http://www.some_domain.cz/category/page_name-1/">left</a>
<a id="pr" href="http://www.some_domain.cz/category/page_name-3/">right</a>
</div>
</body>
</html>
I have this one hack - in PHP I know on which page I am, which page is before and which is next, I add this links it to arrows (which have tag "a"). This function working perfect. But I use this functional code to get correct URL => I don´t need some big function in JQuery which can split actual url with RegEx, know on which page I am, know if exist next page etc... Now I must only style this code to my page, but this is working.
I thank Omar for his time and I close this topic.

Jquery datepicker not working after postback

It works at first open of the page but when i navigate to other pages that uses also the datepicker. It no longer works. Did i missed something in my code? Please see below. Thank you so much.
<link href="css/custom-theme/jquery-ui-1.10.3.custom.css" rel="stylesheet" />
<script src="js/jquery-1.9.1.js" type="text/javascript" language="javascript"></script>
<script src="js/jquery-ui-1.10.3.custom.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
$('#btnDfrom').focus(function () {
$(this).datepicker();
});
$('#btnDto').focus(function () {
$(this).datepicker();
});
</script>
<span id="filtertxt">Date From: </span>
<input type="text" id="btnDfrom" />
<span id="filtertxt">Date To: </span>
<input type="text" id="btnDto" />
This one does not work also
$(function(){
$('#btnDfrom').datepicker();
$('#btnDto').datepicker();
});
Jquery UI datepicker isn't intended to be called multiple times on the same element, which is what would happen if you call it on a focus event.
All you need to do is call it once on the target element, like so:
$(function(){
$('#btnDfrom').datepicker();
$('#btnDto').datepicker();
});
The datepicker plugin will take care of handling clicks and focus events on the elements by itself, you don't need to.
EDIT: You should also check that you are both including the script files, css files and this code on each page where you use the datepicker (but make sure it's only included once!)
your code is looking cool
$(function(){
$('#btnDfrom').datepicker();
$('#btnDto').datepicker();
});
check your code I think may be jquery is Conflicting somewhere in your pages.

ASP.NET MVC 4 - Razor Within Javascript Function Within #Section Causes Scope Error

The problem is that placing a C# variable within a JS function within a #Section produces incorrect javascript (in MVC 4 only).
In a JS function in MVC 3, everything executes as expected.
#section test {
<script type="text/javascript">
$(function () {
alert(#DateTime.Now);
});
</script>
}
Take this exact same code and place it in an MVC4 app and you will get malformed JS.
The HTML on the page will actually render the following:
<script type="text/javascript">
$(function () {
alert(12/27/2011 11:04:04 AM);
and the html will emit
); }
Note the closing script tag is not produced.
It appears the closing curly brace in the JS function is confused the be the closing curly brace in Razor.
Note that I am also declaring a RenderSection("test",false") in my _Layout.cshtml file.
Slightly old reply, but I was experiencing the same issue, soI have submitted a feedback report on Microsoft.Connect hopefully it should get sorted before full release.
https://connect.microsoft.com/VisualStudio/feedback/details/720079/mvc-4-javascript-in-section-issue
I know have been a wile since the question was asked, but you can also try something like this.
#section test {
<script type="text/javascript">
$(function () {
alert(#(DateTime.Now));
});
</script>
}
Note the "()" after the #.
Hope this help.
Sorry, I just couldn't help but chime in here... I don't see how the syntax in the initial question would actually work. When I try it in MVC3, in VS2010, it puts a green squiggly line under the "DateTime", and when you hover over that, it says "Conditional compilation is turned off". But if I surround it in quotes, it does work, as in:
#section test {
<script type="text/javascript">
$(function () {
alert("#DateTime.Now");
});
</script>
}
And then when I run it (F5) it displays the JavaScript alert as expected, and when I view source it renders:
<script type="text/javascript">
$(function () {
alert("07/09/2012 10:41:26");
});
</script>
What I ended up doing for now was emitting the closing JS func in #Html.Raw().
Since this is so easy to recreate, I'm going to submit this as a bug to Microsoft.
#section test {
<script type="text/javascript">
$(function () {
alert(#DateTime.Now);
#(Html.Raw("});"))
</script>
}
I've seen problems like this. Wrap your alert statement in <text></text> tags
Definitely a bug. I prefer this workaround over the one you posted. For some reason, other razor elements seem to interpret the brackets correctly, so I simply wrap the script content in another set of braces.
#section test {
if (true) {
<script type="text/javascript">
$(function () {
alert(#DateTime.Now);
});
</script>
}
}

Resources