Mvc change action - asp.net

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")";
}
});

Related

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;
});
}

How to read Model values and send them by ajax post

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

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'}";

calling asmx service using jquery ajax asp.net 4.0

I'm trying to call a sample asmx service using jquery, here is the jquery code
$.ajax({
type: "POST",
url: "/Services/Tasks.asmx/HelloWorld",
data: "{}",
dataType: "json",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert(data);
}
});
This is not showing any message,code is in asp.net 4.0,
Am I missing any thing?
Edit - I changed the dataType to xml, now success function is working it return following xml
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>
I'm using following code to parse xml data and it is showing null in alert
success: function (data) {
edata = $(data).find("string").html();
alert(data);
}
I believe it's because you have the dataType: "json" and it's expecting the response content-type to be the same but XML is being returned. I bet the complete event is being raised but not success.
try
$.ajax({
type: "POST",
url: "/Services/Tasks.asmx/HelloWorld",
data: "{}",
dataType: "json",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert(data);
},
complete: function (data) {
alert(data);
}
});
UPDATE
I think it's because you're using .html(), you need to use text(). Also i don't know if you meant to do it or not but you have data in your alert, i'm assuming you meant to use edata. The following worked for me:
jQuery.ajax({
type: "POST",
url: "/yourURL",
dataType: "xml",
data: "{}",
contentType: "application/xml; charset=utf-8",
success: function(data) {
edata = $(data).find("string").text();
alert(edata);
}
})
I'd recommend adding the [ScriptService] attribute to your Tasks.asmx class so it will accept and respond in JSON instead of XML. Your client code looks good, but you'll want to take a look at "data.d" instead of "data" in your success handler.
use it.
<script>
alert("aaa");
$.ajax({
type: "POST",
url: "MyService.asmx/HelloWorld",
data: "{}",
dataType: "xml",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert(data);//data-object xmldocument
edata = $(data).children("string").text();
alert(edata);
}
});
alert("bbb");
</script>
Well, you're stating that the dataType is JSON, but the contentType is XML. Try
contentType: "application/json; charset=utf-8",
If not, then we'd have to see the asmx code.

Why does this JQuery call to asp.net pagemethod load the whole page?

Here is a snippet of my html:
<input id="btnGetDate" type="submit" value="Get Date" />
<div id="Result"></div>
<script type="text/javascript">
$(document).ready(function() {
$("#btnGetDate").click(function() {
$.ajax({
type: "POST",
url: "Date.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#Result").text(msg.d);
}
});
});
});
</script>
My Page Method id defined as follows:
[System.Web.Services.WebMethod]
public static string GetDate()
{
return DateTime.Now.ToString();
}
When I click the Get Date button, I saw the date flash on the screen for a second, but since the whole page is loading, it disappears and when I view it in firebug, I see it is doing the POST, but quickly disappearing. Any ideas on how to resolve this?
Try returning false from your $("#btnGetDate").click() event handler:
$("#btnGetDate").click(function() {
$.ajax({
type: "POST",
url: "Date.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#Result").text(msg.d);
}
});
return false;
});
karim79's solution will do the job in Internet Explorer - but to make sure that it works in Firefox and other browsers as well, you probably want to add an input argument to the click handler that will take the click event, and stop the event.
$("#btnGetDate").click(function(ev) {
ev.stopPropagation();
$.ajax({
type: "POST",
url: "Date.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#Result").text(msg.d);
}
});
return false;
});

Resources