I am facing issue while returning Json values to View. I don't know why it is happening may be issue with JQuery. Below is my code for reference. Its not executing the success parameter of AJAX.
<script type="text/javascript">
var $edituserform = $("#edituserform");
$(document).ready(function () {
$('#userList').DataTable();
});
function updateuser(id)
{
var myUrl = '@Url.Action("EditUser", "Admin")';
//var myUrl = '/Admin/EditUser?Id=' + id;
alert(id)
$.ajax({
url: myUrl,
type: 'GET',
data: {Id:id},
dataType: 'json',
sucess: function (result) {
alert('inside success')
//$edituserform.html(result.partialView);
$edituserform.load(result);
}
})
}
---- Controller ----
public ActionResult EditUser(int Id)
{
try
{
var objUser = objUserLogic.GetUserById(Id);
//return Json(new { partialView = MvcHelper.RenderPartialView(this, "_EditUser", objUser, null) }, JsonRequestBehavior.AllowGet); //tried this commented code first
return PartialView("_EditUser", objUser);
}
catch (Exception Ex)
{
return View("ViewUsers");
}
}
A PartialView
is not JSON, which you're expecting. Change dataType
to text/html
or return a JsonResult
, ie Json( someModel )
Controller Method
[HttpGet]
public JsonResult EditUser(int dil)
{
var objUser = objUserLogic.GetUserById(Id);
var jsonparse= objUser
.Select(x => new //This is trick that prepare json array
{
name=x.name,
Id=x.Id
});
return Json(jsonparse, JsonRequestBehavior.AllowGet);
}
Ajax Method
$.ajax({
url: "/controllerName/getkat",
type: "GET",
cache: false,
contentType: "application/json",
data: { Id: xxx},
dataType: "json",
success: function (data) {
if (data.length > 0) {
//Parse Response
$.each(data, function (i, state) {
$('<option>', {
value: state.Id
}).html(state.name).appendTo($select);
});
}
},
complete: function () {
},
error: function () {
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.