I need a little help.
So I'm trying to call a lot of partial views from the same list model, each using a different filter. Is there anyway for me to pass the Datafilter value from the views into the model?
$(document).ready(function() {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function() {
$.ajax({
type: "POST",
url: "ControlerFolder/Controler",
data: {someParameter(Filter): "value"},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
msg is the json that you can return
}
});
});
});
If you need more help say something.
Related
I work with asp.net and I have some issues.
I created partial view with error message and I want to call view using jQuery.
I have already created function in controller which returns a partial view. But I am stuck with ajax, all time I get errors.
Controller:
[HttpGet]
public ActionResult LoadPartialView()
{
return PartialView("_ErrorMessageWindow");
}
jQuery:
$.ajax({
url: "#(Url.Action('LoadPartialView'))",
type: "GET",
cache: false,
datatype: 'html',
success: function (data) {
}
});
Error:
jquery-3.3.1.min.js:2 GET https://localhost:5001/#(Url.Action('LoadPartialView'))?_=1565937416181 404 (Not Found)
Reason for not working:
#Url.Action() is Razor server side code and that will not be parsed in external .js files.
Solution:
Create this <div> in your .cshtml:
<div id="partial-view-url" data-request-url="#Url.Action("LoadPartialView", "ControllerName")"></div>
then get Partial View Url in your separate/external .js file using data-request-url like below:
$("#partial-view-url").data('data-request-url');
So your $.Ajax will become like this:
$.ajax({
url: $("#partial-view-url").data('data-request-url'),
type: "GET",
cache: false,
datatype: 'html',
success: function (data) {
}
});
Another way is to declare global variable:
Create global variable in your main .cshtml file and use it in your external .js file.
//Declare this variable in your main `.cshtml` file.
var partial-view-url = #Url.Action("LoadPartialView","ControllerName");
//and then use it in your separate/external `.js` file.
$.ajax({
url: partial-view-url,
type: "GET",
cache: false,
datatype: 'html',
success: function (data) {
}
});
I am facing a strange problem in my MVC 5 application where I want to pass a Drop Down Selected value to controller using a Ajax post.
the Post code looks like this:
$(function () {
//Change the Value stream list each time the BU is selected
$('#Lob').change(function () {
alert(JSON.stringify($('#Lob option:selected').text()));
$.ajax({
url: '#Url.Content("~/Dashboard/GetValueStreams/")',
dataType: 'json',
type: 'POST',
data: JSON.stringify($('#Lob option:selected').text()),
contentType: 'application/json',
success: function (VSList) {
// do stuff
});
}
});
});
});
The ALERT works fine and displays the selected value correctly. However in the controller, the string appears as null.
[HttpPost]
public ActionResult GetValueStreams(string BUName)
{
// Here the BUName parameter is coming as null.
}
I have tried changing my JSON POST data to the following:
data: {"BUName": JSON.stringify($('#Lob option:selected').text())},
This also does not work. Any help will be much appreciated. Thanks.
Change your data to data: JSON.stringify({BUName : $('#Lob option:selected').text()}).
I tested and it worked.
$.ajax({
url: '#Url.Content("~/Dashboard/GetValueStreams/")',
dataType: 'json',
type: 'POST',
data: JSON.stringify({BUName : $('#Lob option:selected').text()}),
contentType: 'application/json',
success: function (VSList) {
// do stuff
}
});
I have a webforms application and need to make an jquery ajax call to a PageMethod (i.e. WebMethod) in code behind from my aspx page. So far it does not work for me. Is this possible?
Here is my code:
$(function()
{
setInterval(function(){
$.ajax({
type: "GET",
url: "/ClientBillingBillDetails.aspx/MyPageMethod",
data: {someData: '<%= TheData %>'},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
}
});
}, 10000);
});
[System.Web.Services.WebMethod]
public static string MyPageMethod(int someData)
{
return "";
}
Is something wrong with my URL or something else?
Thanks
Try This:
$(function () {
setInterval(function () {
$.ajax({
type: "POST",
url: "/ClientBillingBillDetails.aspx/MyPageMethod",
data: "{ 'someData': '<%= TheData %>' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
}
});
}, 10000);
});
Use type as post and make sure if you have ajax.jquery library reference added in solution.
Also i think you can remove '/' in specifying method..
Just use "ClientBillingBillDetails.aspx/MyPageMethod".
Else you can use simple pageMethods using scriptmanager
Been following some tutorials, as i'm learning C#, however was just attempting to make an ajax call to the server, i'm using my localhost.
As far as I can tell i'm doing its right, but it's obviously not.
Maybe it my folder structure or just the name of the file.
The Default.aspx file is at the project root
Here is my ajax call
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx",
dataType: "json",
data: "{'FirstName':'test','LastName':'test1','City':'test3','EmailID':'test4'}",
success: function (data) {
console.log("Woo!");
},
error: function (result) {
console.log("fail!");
}
});
I know eventually it will have to call a method within the file, but its not even finding it at the moment.
Thanks in advance, James
You can use controller Home and create the one action.
Example:
public ActionResult FirstAjax()
{
return Json(true, JsonRequestBehavior.AllowGet);
}
After that your jquery ajax is:
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "Home/FirstAjax",
dataType: "json",
data: "",
success: function (data) {
console.log("Woo!");
},
error: function (result) {
console.log("fail!");
}
});
So far I have never had the need to send data from ajax to two or more complex type parameters in the server method.
What I am trying to achieve is if I had this action on my controller:
[HttpPost]
[Authorize]
public virtual ActionResult SubmitData(Person myPerson, Chair myChair)
{
//..
}
where Person and Chair are complex types, I want to be able to send data from ajax that is going to bind properly to myPerson and myChair. Something like this:
var personData = { 'Name': Steve, 'Age': 35};
var chairData = {'NumberOfLegs' : 3, 'Color' : red};
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: myUrl,
data: {'myPerson':personData, 'myChair':chairData}
});
I tried so, but it just wont work. Can you help me with that? The call goes to the action but with null values for both arguments.
I have modified the ajax call to use JSON.stringify the parameters works well now.
var personData = { 'Name': Steve, 'Age': 35};
var chairData = {'NumberOfLegs' : 3, 'Color' : red};
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: myUrl,
data: JSON.stringify({'myPerson':personData, 'myChair':chairData})
});