asp.net mvc another controller in the same folder - javascript

I have a folder 'Report' which holds ReportController.
Now i want to add AgreementController to the same folder.
from javascript i have a code which runs 'Download' action (downloads file).
When i run from javascript - Report/Download/id='5' everything fine.
When i run from javascript - Report/Agreement/Download/id='5' everything bad.
How can i run 'Download' action from Agreement controller ?
I dont want one folder-one controller structure.
I think only areas can help me there ?

according to routing:
url: "{controller}/{action}/{id}"
your code is:
Report/Agreement/Download/id='5'
so in your case:
Controller= Report
action= Agreement
Id= Download.
that's why you get an error
You need to remove Report from call end use this:
Agreement/Download/id='5'

If you are writing js in same view page and not in external js file, you can use mvc url.action in ajax call file method to call appropriate controller and action method as described below
syantax:
url: '#Url.Action("ActionMethodName","ControllerName",new {Parameters})'
In your case it would be something like this:
to invoke report controller's download method
url: '#Url.Action("Download","Report",new { id = "Value" })'
to invoke agreement controller's download method
url: '#Url.Action("Download","Agreement",new { id = "Value" })'
If you are using external js file then it will be as below
url: serverbaeurl + "/Report/Download?id=" + "Value"
url: serverbaeurl + "/Agreement/Download?id=" + "Value"
(here serverbaseurl will be the domain of website)
(you will have to add other ajax options as needed)

Related

reference javascript content coming from db instead of static file

I am developping an asp.net web application :
In the website folder, I have a folder called "integrations" containing a list of js files (name1.js, name2.js, name3.js ...)
A user makes a http request to a mvc controller method where he gives as input a "name".
This method gives the "name" as a property of a viewmodel to a razor view containing the following code :
This razor view is returned to the user.
The previous code is working well, but I would like to add an improvment. Actually you need to add a js file inside the folder integrations,
and you cannot do that when the application is running in production.
So I would like instead of having a script tag referencing a file placed inside the integrations folder to have a script tag containing a js content
coming from a data table like this :
name : jscontent
name1 : jscontent1
name2 : jscontent2
But I don't know how to changed this :
<script src="~/integrations/#(Model.Name).js"></script>
To :
<script>>query in db by #(Model.Name) parameter to get corresponding jscontent</script>
Make it a controller/method and you can take advantage of things like caching, auth etc for free:
public class ScriptController
{
public ActionResult Content(MyModel model)
{
var result = "alert("Hello World!");";
return JavaScript(result);
}
}
JavaScriptResult
Then in your html:
<script src="~/Script/Content?prop1=whatever"></script>

Mismatch Url from Url.Content

Through Ajax I am trying to hit action method (city) of controller (Home) using below code
url: '#Url.Content("~/Home/city")',
from the address bar I found that the Url is mismatched as I am in different controller. For example if I am in site controller then it is redirecting to site/Home/city. In place of this it should redirect to Home/city. Means its taking the current controller and the url which I am passing through Url.Content.
If I place javascript code in .aspx then the Url getting redirect correctly, if the javascript code is in separate file it raising the above mentioned issue.
How to redirect to particular Url from javascript?
If your JavaScript is in an external file you can use the following technique to access the route path to your action.
Simply attach the url to the element which invokes the ajax call as a data attribute.
In the below example I attach it to an input button.
<input data-url="#Url.Action("city", "Home")" value="DoPost" />
Then from within your event handler you can use jQuery data method to read the attribute i.e.
var superUrl = $(this).data('url');
Then use this in your ajax call:
url: superUrl

Pass Javascript Variable into createlink method call Grails

var search= document.getElementById('appMenu').value
document.location.href= '${createLink(controller: 'application' , action:'ajaxAppSearch', params: ['query': search])}'
The element appMenu is a text field, so I am getting the value that the user enters into the text box to pass into a search controller. However, it keeps telling me that the params query is null. It seems that search isn't being passed into the create link method. Anyone have a suggestion?
Grails (controllers, GSP and tags, etc) are working on server side. JavaScript on client side. And this link is prepared before sending data to browser, and before JavaScript can pass its variable into GSP tag.
But you can prepare base link on server side, and add extra parameter on client side, by using javascript, like:
var search= document.getElementById('appMenu').value;
document.location.href= '${createLink(controller: 'application' , action:'ajaxAppSearch')}?query=' + escape(search);

Integration beetween a compositeComponent's attribute and a Servlet in JSF

I'm trying to integrate Plupload with JSF in the most independent way possible, for that I created a composite component.
To use Plupload in my JSF page i just call:
<comp:plupload ... value=#{MyBean.files} />
Where MyBean.files is a list.
When I add files to Plupload component and click the button "start upload" I want it to upload everything to a temporary folder and fill the object specified in "value" of my composite component with these files properties (path, for instance).
To upload the files i'm using a servlet, it has nothing to do with JSF, it works fine for the 1st part: it uploads everything to a temporary folder. My problem is the second part, I did a lot of research, but I can't find a way to communicate the attribute "value" in my JSF composite component to my servlet.
Plupload uses javascript to configure everything, the request will be sent to the URL specified in the attribute "url" in the following code:
<composite:interface>
...
<composite:attribute name="value" required="true" />
</composite:interface>
<composite:implementation>
...
<script type="text/javascript">
// Convert divs to queue widgets when the DOM is ready
$(function() {
$("#uploader").pluploadQueue({
// General settings
runtimes : '#{cc.attrs.runtimePreferences}',
url : '/plupload',
max_file_size : '#{cc.attrs.maxFileSize}',
...
});
});
</script>
<div id="uploader">
<p><h:outputText value="Your browser does not support this." /></p>
</div>
</composite:implementation>
I specified "/plupload" as url, that's my servlet's url (in web.xml).
I can think about two possible solutions:
Continue using this servlet, which is completely independent from FacesServlet, and find a way to pass the attribute "value" in my composite component as a request attribute to my servlet. But, how can I do this?
Stop using a new servlet, I should't do this since I'm using JSF, everything should be processed by FacesServlet. Instead of using a new servlet, i could create a ManagedBean. Inside this managedBean I could create a method and recover the HttpRequest and HttpResponse. It's easier to comunicate my compositeComponent with the method who handles the upload if it's a managedBean. Problem: How can i reference a managedBean's method through an URL? I still have to fill the attribute "url" in the javascript code. Is there anything like "/faces/MyBean?action='myMethod()'?
Thank you in advance for any answer ;)
Send it as request pathinfo.
url: '/plupload/' + encodeURIComponent('#{cc.attrs.value}'), // You might want to escape JS special characters like singlequotes, newlines, etc as well, depending on what the value can contain.
If the servlet is mapped on /plupload/* then you can obtain it as follows:
String value = request.getPathInfo().substring(1);

Use Separate js File And use Url Helpers in it with ASP.NEt MVC 3 and Razor View Engine

I ask a similar question here and Darin Dimitrov answer that we can't use Url helper like $.ajax({ url: '#Url.Action("Index")', . . . in separate js file so what is your suggestion to use Url helper in view page and pass it to javascript, I don't want to use hard code url, I need to find it with Url helper.?
Use a hidden field to store your url, then use javascript to read the hidden field, then use that in your code. That way you can keep the JS file separate to the view. Something like this:
//In Your View
#Html.Hidden("MyURL", Url.Action("Index"))
//In Your JS
var myUrl = $("#MyURL").val();
$.ajax({ url: myUrl , . . .
The easiest way is just to create a global variable called something and just reference to it in your external JS
var baseURL = '#Url.Action("Index")';
Inside your external JS
$.ajax({ url: baseURL + "Action"
You can use RazorJS for that purpose. It allows writing Razor-Style C# or VB.NET inside your JavaScript files. There is a short description available here.
There is no need to have hidden field, even this works too in the external .js file.
var myUrl = /ControllerName/ActionName;
$.ajax({ url: myUrl , . .
Take a look at Generating External JavaScript Files Using Partial Razor Views. In this blog post, I describe how you can make use of regular Razor views and a custom action filter to render external JavaScript files that can have Razor code in them.
I used a similar approach to raklos, but was looking to get the root directory path in all places, so I went with the code below.
#Html.Hidden("AppUrl", Url.Content("~"))

Categories