How to read Model values and send them by ajax post - asp.net

I have the following code and it`s pointing out errors as follows
Error 1 The name 'date' does not exist in the current context
Error 2 The name 'person' does not exist in the current context
What is wrong?
$("#test").Click(function () {
var date = $("#DateFrom").val();
var person = Model.SelectedPerson;
$.ajax({
url: '#Url.Action("testEmp","Employee",new {dateFrom = date, selectedPerson= person})',
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (result) {
$('#text).html(result);
},
});
return false;
});

Try this:
$.ajax({
url: '#Url.Action("ActionName")',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ date: '..', person: '...' }),
success: function(result) {
}
});
EDIT: Take a look at this answer for the complete solution.
jquery ajax forms for ASP.NET MVC 3

Related

Mvc change action

I have a add patient page. When user click add patient button, I insert data to database and open treatment page. How can i do it? I tried change page in javascript but did not work:
$.ajax({
type: "POST",
url: "#Url.Action("InsertPatient", "Addpatient")",
data: JSON.stringify({ "isim": isim, "soyisim": soyisim, "tc": tc, "cinsiyet": cinsiyet, "telefon": telefon, "telefon2": telefon2, "kangrubu": kangrubu, "dogumtrh": dogumtrh, "referans": referans, "doktor": doktor, "adres": adres }),
contentType: "application/json; charset=utf-8",
success: function (response){
#Layout = "~/Views/Treatment/Index.cshtml";
}
});
and I tried add last row in InsertPatient function in controller:
RedirectToAction("Index", "Treatment");
You cannot put a server side code inside the onSuccess function and expect it to work.
IF you need to redirect from JavaScript then use window.location = 'URL'
$.ajax({
type: "POST",
url: "#Url.Action("InsertPatient", "Addpatient")",
data: JSON.stringify({ "isim": isim, "soyisim": soyisim, "tc": tc, "cinsiyet": cinsiyet, "telefon": telefon, "telefon2": telefon2, "kangrubu": kangrubu, "dogumtrh": dogumtrh, "referans": referans, "doktor": doktor, "adres": adres }),
contentType: "application/json; charset=utf-8",
success: function (response){
window.location = "#Url.Action("NewActionName","ControllerName")";
}
});

Subscribe to specific event in ASP.NET WebHooks

I'm trying to learn ASP.NET WebHooks, but the docs are pretty sparse right now.
What I'm trying to do is subscribe to a specific events. All the samples I can find demonstrate subscribing to all the events, which is not very useful for me.
EDIT:
This is the code for subscribing I found in the docs:
function subscribe() {
$.ajax({
type: "POST",
url: "/api/webhooks/registrations",
data: JSON.stringify({
WebHookUri: "http://localhost:59927/api/webhooks/incoming/custom",
Secret: "12345678901234567890123456789012",
Description: "My first WebHook!"
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data, status) { alert(status); },
failure: function(errMsg) { alert(errMsg); }
});
return false;
}
What should be the code for subscribing to a "BookAdded" event?
Thanks!
So, for anyone else looking for the answer, this is how it should be done:
function subscribe() {
$.ajax({
type: "POST",
url: "/api/webhooks/registrations",
data: JSON.stringify({
WebHookUri: "http://localhost:59927/api/webhooks/incoming/custom",
Secret: "12345678901234567890123456789012",
Description: "My first WebHook!",
Filters: ["BookAdded"]
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data, status) { alert(status); },
failure: function(errMsg) { alert(errMsg); }
});
return false;
}
Note the Filters field added to the ajax statement.

Displaying JSON on HTML returned from Web Api Controller

I have created the Web Api project with MVC.I want to display the JSON result in HTML page.
How should i do that?
URL:
http://localhost:58379/api/ccpayment/GetEligibility?userid=3830&type=json
and the data i am getting is in the following format
{"DailyAllowance":10000.00,"AmountUsedForTheDay":0.00,"CDF":3.0000,"UserEligibleForCC":"EligibleButBanned"}
function getYourData() {
$.ajax({
url: "/yourAPI_URL",
data: JSON.stringify({ yourPostDataIfAny }),
type: "Post",
datatype: "json",
contentType: "application/json; charset=utf-8"
}).then(function (data) {
var response = $.parseJSON(data);
//your input field on page
txtAllowance.value = response.DailyAllowance;
});
}

ASP.NET MVC Controller - 500Error

Why isn't my controller function called?
I always get 500error (in fiddler). I get no error in Visual Studio or an error site.
Controller:
[POST("/test1")] // attributerouting (works with GET methods)
public ActionResult test1(TreeViewItemModel aItem)
{
...
}
Client:
var tree = $("#demo2").jstree("get_json");
var c = JSON.stringify(tree);
$.ajax({
type: "POST",
url: "/test1",
data: tree,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
}
});
Some times 500 Internal Server Error occurred because of syntax error in the View of it's controller like { or } mismatch or etc. Did you check syntax of test1.cshtml?
problem was the data format:
solution:
public ActionResult test1(IEnumerable<TreeViewItemModel> aItem)
{
}
Client:
var tree = $("#demo2").jstree("get_json");
var c = JSON.stringify(tree);
$.ajax({
type: "POST",
url: "/test1",
data: c,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
}
});

asp.net page method with jquery and parameter

In my javascript, I have:
var testdate = "{'TheNewDate' : '12/02/2011'}";
$("#mydiv").click(function () {
$.ajax({
type: "POST",
url: "../Pages/Appointments.aspx/GetAppointements",
data: testdate,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFn,
error: errorFn
});
});
In my code behind I have
[WebMethod]
public static string GetAppointements(string DateInput)
{
var t = DateInput;
However, when I click to run the call, I get the error function to activate. When I change the code behind function to public static string GetAppointement() it works. But I guess my goal is to pass a parameter to the code behind. What am I missing?
Thanks.
Your parameter is called DateInput and not TheNewDate, so:
$('#mydiv').click(function () {
$.ajax({
type: 'POST',
url: '../Pages/Appointments.aspx/GetAppointements',
data: JSON.stringify({ dateInput: '12/02/2011' }),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: successFn,
error: errorFn
});
});
You should make your JSON data match the parameter name in the web service method.
var testdate = "{'DateInput' : '12/02/2011'}";

Resources