asp.net mvc url to string - javascript

i am creating mvc application and is it possible to get the url to string somehow?
my url is like:http://localhost:7264/Asortiman/Browse?kategorije=327
and in the head of some view i would like to get this url like string and take last 3 digits in this case 327 and use it as param in my function.

Why dont you do it at the Controller level and send it with the ViewBag dynamic object?
I Suppose your Controller is Asortiman and your Action method is Browse. Then if you define your method like;
public ActionResult Browse(int kategorije){
ViewBag.KategoriJe = kategorije;
return View();
}
Then at the view you can now reach it with the same dynamic object. For further use see the default mvc application project at the vs2010

Related

Passing javascript variable to spring MVC controller with request param

Is it possible to send a javascript variable to a controller endpoint, and then have the controller return a new view?
I've tried using a requestbody and ajax to do it, which passes the variable correctly, but is unable to load a new view.
Maybe there's a way to do it with thymeleaf?
If you're using Thymeleaf then just reference the template you want to return to the user by its filename.
#Controller
public class YourController {
#GetMapping("/someUrl")
public String getTemplate(#RequestParam String templateName){
return templateName;
}
}
This would be the bare minimum that you'd need, assuming your templates are in the correct folder - by default in resources/static. If the frontend sends a GET (/someUrl?templateName=xyz) to the backend, it would return the xyz.html from your templates. If it does not find the template that was requested in the parameters then it will return a 404.
Edit: Reading through the comments I realized there might be a confusion between #RequestParam and #PathVariable. In case you want to use a path variable to define the template name, so that the frontend can call GET (/someUrl/xyz), then you can do
#Controller
public class YourController {
#GetMapping("/someUrl/{templateName}")
public String getTemplate(#PathVariable String templateName){
return templateName;
}
}
Resources for both below:
https://www.baeldung.com/spring-request-param
https://www.baeldung.com/spring-pathvariable

How to pass a div element as a parameter to an MVC controller?

how can i pass a <div>Hello World</div> using jQuerys $.post() function to an MVC controller like public JsonResult GetDiv(string element)
I've tried encodeURL() on the client but I keep getting an Internal Server Error(500)...i'm basically trying to save HTML code to SQL Server. Thanks.
It's very likely that you'll need to decorate the specific property that you are attempting to pass with the [AllowHtml] attribute as seen below on the property that you are attempting to use HTML for:
public class HtmlContent
{
[AllowHtml]
public string Content { get; set; }
}
And then simply bind the to that property within your Controller Action:
public JsonResult GetDiv(HtmlContent element)
{
// Access your element here
var html = element.Content;
// Save to the database
}
This will let .NET know that this string is expected to contain HTML content, which it may otherwise see as potentially malicious content and reject (similar to the 500 error that you are experiencing).
On parent element of div, call innerHTML, this will return the HTML as a string, which you can then post.
could you provide more details?
Where is your controller running?
Is there any specification of your Controller youre posting data to?
In order to get the HTML code of your jQuery div use the .html() function - link
The post function works as follows:
jQuery.post( url [, data ] [, success ] [, dataType ] )
The second argument must be a string or a plain object. Therefore using the html()-Function should work.
Here is a fiddle I made (basicly a copy pasta of the jsfiddle.net echo demo + jquery post demo): https://jsfiddle.net/vyrdp7uo/

asp.net mvc passing any data from a partial view that is rendered with $.get() to ContentPage

I am trying to make my web application like a desktop application.
I am also not using any _LayoutPage and #RenderBody().
I have a ContentPage as MasterPage and a tag named main
I am using ajax get method to render my views or partial views like this:
$.get(url).done(function (result) {
$("main").html(result);
});
I managed to inject my script and css files with javascript functions.
And now I want to pass some specific datas without using javascript functions.
It can be via using ViewBag, I guess.
I want to pass that data from my partialView:
ViewBag.BodyClass = "signup-page";
to my MainPage like this:
<body class="#ViewBag.BodyClass">
How can I do that?
A little note: Please ignore that I am a newbie and my low reputation
If you have a script manager ($.get) that calls your server to get the views and partial views, no problem.
When you request a URL, normally MVC calls a Controller and Action. In that action you can return content, view, partial view, file and so on...
You can create a new instance of a class model and pass to your partial view.
public ActionResult Index(string parameter1, string parameter2)
{
var model = new Models.ModelTest();
model.BodyClass = "some class";
return PartialView("_Page", model);
}
You will call some like this:
$.get("http://localhost/app/getviews?id=3422&parameter1=test&parameter2=foo")
In your view or partial view:
#model YourApp.Models.ModelTest
<body class="#Model.BodyClass">
I use that all the time.
I wrote that code on my partialView. It adds a class at ContentPage's body tag
$("body").addClass("signup-page");

URL string parameters passed to MVC controller action arrive as null

Have Controller:
public class MyController : Controller
{
[HttpGet]
public ActionResult MyAction(int iMode, string strSearch)
{
return View();
}
}
In my view I have a div with id=center
I execute the following code in javascript
url = "/MyController/MyAction?iMode=7&strSearch=as";
$('#center').load(url);
When debugger his the breakpoint in my action on first line, iMode variable shows proper value of 7, strSearch parameter arrives as null.
Any help/advice would be most welcome.
Just use the ampersand instead of &
url = "/MyController/MyAction?iMode=7&strSearch=as";
Thanks Innatepirate for the tip. Wasted enough time trying to figure out why controller is getting null values. Replacing & with ampersand did the trick. By the way I was doing the simple age old window.location.href = link and still had the problem. Probably its the MVC parsing and routing that is messing this up.

passing an javascript object from cakephp view to controller

I am developing a website using cakephp, and I'd like to pass a javascript object in view back to the controller. I know that using a form could be easier but I need to do this customized.
So here's the object ('annotation' and 'article_id' are real column names in the database, annotation and article_id are both variables containing data):
var postdata = {
'annotation' : annotation,
'article_id' : article_id
};
What method in the view should I use? Is it .post?
And how should I program the corresponding controller to correctly receive the object and extract data from it?
You probably want to do an ajax request.

Categories

Resources