Visual Studio Mac appsettings.json allow unobtrusive javascript - javascript

#Ajax is not being recognized in razor views for my MVC application. I believe the reason is bc Unobtrusive Javascript isn't allowed for the newest version of VS Mac?
To fix this problem "the web" advises adding below to the web.config of the project.
enable unobtrusive JS screenshot
However .Net Core doesn't have that, now has appsettings.json, which looks like this:
appsettings.json screenshot
Anyone know how to allow unobtrusive-Javascript in JSON?

In Asp.Net core, When you use the input tag helpers to generate the input fields, it does generate the html 5 attributes on those inputs used by jQuery validate & unobtrusive validate plugin, based on the data annotations you have on your view model properties. As long as you include the needed 2 javascript files in your page, the client side validation will work. You do not need any web config setting or anything like that.
Just include these 2 files to your page/layout
<script src="~/js/jquery.validate.min.js"></script>
<script src="~/js/jquery.validate.unobtrusive.min.js"></script>
Assuming the 2 files exist in the js directory in the static content root (wwwroot directory by default)
Assuming you also have the span element for showing the validation errors along with your inputs.
<form asp-action="Create" asp-controller="User" method="post">
<input type="text" asp-for="Code" />
<span asp-validation-for="Code" class="text-danger"></span>
<input type="submit" />
</form>
These 2 files are dependent on jQuery. So make sure you included that script as well.

Related

what is rootpath in javascript src attribute?

I'm developing a .net web application. I want to link to a js file in one of my user control. I tried ./, I also tried ~, neither works. what is rootpath in src attribute?
<script src="#Url.Content("~/Scripts/filename.js")"></script>
Depends on what .net version you're working in. This is MVC 3 Using the Razor Syntax
Just drag and drop that js file in your control it will automatically link your js file to the control without any error.

How to get visual studio to recognize a cshtml file as javascript

I am writing a css/js page that has some dynamic parts in it.
To do this i am using a cshtml file containing css/js - i am using mvc.net and returning the css from a controller action.
The trouble is visual studio recognizes this page as html and not as javascript/css so it does not give me javascript/css coloring and IntelliSense.
My questions:
is there a better/easier way of creating dynamic css/js in .net
How can i get visual studio to recognize a cshtml page as javascript.
I know this is an old post...but...
What I did was just put the script tags around my javascript files in their own .cshtml file.
I created a separate controller (JavascriptController.cs), and I created a filter on that controller that removes the script tags. I set the filter in the OnActionExecuting method. by just doing
this.Response.Filter = new ScriptFilter(Response.Filter, Response.ContentEncoding);
So you get the syntax, you get razor without having to use RazorJS, and you can request the js files like regular routes in an MVC application. You just have to keep the script tags on the partial view while editing.
So you can call
/Javascript/{Action}
and you'll get your javascript file with your razor in it, and the filter will remove the script tags so you can include it like a normal script.
<script src="http://{host}/Javascript/{action}"></script>
What is wrong with putting <script> and <style> tags in a page and put your dynamic js/css there , in the end if it is dynamic there is no way for caching it so this approach will be fine.
you can write something like below:
<script>
function myFunction_#MyFunc(params)(obj) { return obj.field + #MyOtherFunc(params); }
<script>
and razor engine will evaluate #MyFunc(params) and #MyOtherFunc(params) before sending it to browser
The best solution is to follow unobtrusive javascript and unobtrusive styling and put your javascript into a .js file, put your css into a .css file and reference them in the markup in your cshtml file with a <script> and <link> tag.
e.g.
<script src="Scripts/scriptName.js" type="text/javascript"></script>
<link rel="stylesheet" href="Content/styleSheetname.css" type="text/css" />
This is good practice as it keeps your content(markup)/styling/behaviour separate.
For JavaScript you could try the RazorJS nuget package. But we've run into some inconsistencies while using it.
Still trying to find a better way to do this using Controller/Views and still be able to use intellisense and get decent coloring.

Upload a file into SharePoint using FORM POST

I would like to upload a file into share point using FORM POST. I am trying in this way
<FORM NAME="oForm" id="oForm" ACTION="<site>/_layouts/Upload.aspx?List={A4793E2B-3081-4668-B6F1-0A013159F9B1}&RootFolder=/sites/servdeldocmgr/Test SOAP2" ENCTYPE="multipart/form-data" METHOD="POST">
<input type="file" name="file1" id="file1" />
</FORM>
In onclick of a button I am doing form post. But It's not uploading. I found .net based solutions but my case is only with javascript and html.
The best is to rely on SharePoint API, populate the item (e.g SPList) and upload the file. I will provide some sample code later as I can't recall the exact function.
EDIT:
http://msdn.microsoft.com/en-us/library/dd587349(office.11).aspx has some good code snippets to get you started.
I am able to add attachment to list item. I put my experience here. Instead of uploading to upload.aspx, I achieved differently. I am able to achieve without .net, I found many .net solutions but again it will be one more programming language in application stack so I didn't use it.
http://myveda-yvb.blogspot.com/2010/01/my-learnings-sharepoint-web-services.html

can't get html td element using javascript in jsf application

and in JavaScript
document.getElementById('myId');
This is not working in JSF application. However, the same is working if I save the generated HTML in my system and open it .
Any Help ?
When writing JavaScript code for a component based MVC framework which generates HTML, like JSF, you should not focus on the source code of the component based MVC framework, but on its generated HTML output.
If you can't tell this beforehand based on the source code, then you need to just open up the page in your favourite webbrowser and then rightclick the page and choose View Source. You'll see that the generated Client ID's are prepended by the ID's of the UINamingContainer components (like h:form, h:dataTable and f:subView). If you don't specify an ID for each of them, you will get an autogenerated ID like j_id_xxxx. To ease the work, you need to specify an ID for them. E.g.
<h:form id="form">
Also see this blog article for more information and hints. This blog article may also be useful to learn more about the wall between Java/JSP/JSF and JavaScript.

Does anyone know of a good article for creating MVC based components/ extension methods?

I've a couple of extension methods I've been developing for a couple of projects, they currently rely heavily on some AJAX to make bits and pieces work. The problem is that they require copying and pasting JavaScript files to the project you want to use it in.
As this JavaScript file only needs to be used once (all instances of the rendered control use the same file) I'd like to do something like add the script element to the headers collection of the page it's used on via a web-resource (embedding the file as a resource in the assembly). In Web-forms this wasn't a problem - you could add a script block to the headers with a specific ID and simply check for it on page load.
What's the MVC equivalent - is there an equivalent?
I'd like a solution that doesn't require the consumer to copy and paste/ add lines to pages or config...any thoughts?
Stephen Walther has some very good articles on MVC, including Html Helpers.
http://weblogs.asp.net/stephenwalther.
A great place to see Html Helpler code is the MVC source code available at
Codeplex.
There is a tutorial at www.asp.net/mvc on Html Helpers
Here ya go, this guy wrote a custom FormlessScriptManager that will let you register scripts even when there is no <form runat="server"> in your page.
http://developmentalmadness.blogspot.com/2008/04/abstracting-systemwebuiscriptmanager.html

Categories