i have the following action method, that returns a partial view `_create`. but is there a way to pass a Json object such as `return Json(new { IsSuccess = "True" }, in addtion to the Partial view. Part of my Action method looks as follow:-
try
{
if (ModelState.IsValid)
{
var v = repository.GetVisit(visitid);
if (!(v.EligableToStart(User.Identity.Name)))
{ return View("NotFound"); }
vlr.VisitID = visitid;
repository.AddVisitLabResult(vlr);
repository.Save();
ViewBag.LabTestID = new SelectList(repository.FindAllLabTest(), "LabTestID", "Description", vlr.LabTestID);
// return Json(new { IsSuccess = "True" }, JsonRequestBehavior.AllowGet);
@ViewBag.status = "Added Succsfully";
return PartialView("_create",vlr) ;
}
}
Thanks for your reply; But currently i am returning a partial view because it will contain any validation errors that might occur after submitting the new object the new object - incase the client browser does not support client side validation ofcourse-
,, so if i will only return the Json data ,, then incase a validation error occurs it will not be displayed on the view.
Please explain what are you trying to do. Do you want to call tha action with an AJAX request and return Json and in case the javascript is disabled return the view?
If not then do as Andrei said and add that property as a hidden field in the partial view. You can read it then from JavaScript and decide what to do.
Please click 'Mark as Answer' if my reply has assisted you
2. after succfully receiving the response from the server ,, the Onsuccess script will be executed,,, the script simply disable the form:-
function disableform(id) {
$('#' + id + ' :input').prop("disabled", true);
}
The problme is that the script will always disable the form even is some validation error occurs,,so what i was trying to achieve is to return a JSON with the partial view that indicate if the
ModelState.IsValid was valid or not ,,
and if it was not valid to keep the form enabled to allow the user to correct the validation errors.
johnjohn1231...
Participant
922 Points
871 Posts
Can i return a Partial view and a Json object from my action method at the same time
May 02, 2012 11:54 PM|LINK
i have the following action method, that returns a partial view `_create`. but is there a way to pass a Json object such as `return Json(new { IsSuccess = "True" }, in addtion to the Partial view. Part of my Action method looks as follow:-
try { if (ModelState.IsValid) { var v = repository.GetVisit(visitid); if (!(v.EligableToStart(User.Identity.Name))) { return View("NotFound"); } vlr.VisitID = visitid; repository.AddVisitLabResult(vlr); repository.Save(); ViewBag.LabTestID = new SelectList(repository.FindAllLabTest(), "LabTestID", "Description", vlr.LabTestID); // return Json(new { IsSuccess = "True" }, JsonRequestBehavior.AllowGet); @ViewBag.status = "Added Succsfully"; return PartialView("_create",vlr) ; } }BR
ignatandrei
All-Star
134511 Points
21576 Posts
Moderator
MVP
Re: Can i return a Partial view and a Json object from my action method at the same time
May 03, 2012 03:21 AM|LINK
Return only the data and isSuccess . The html part will be on the client side. Please see http://bit.ly/mvc_ajax_jquery
johnjohn1231...
Participant
922 Points
871 Posts
Re: Can i return a Partial view and a Json object from my action method at the same time
May 03, 2012 02:12 PM|LINK
Thanks for your reply; But currently i am returning a partial view because it will contain any validation errors that might occur after submitting the new object the new object - incase the client browser does not support client side validation ofcourse- ,, so if i will only return the Json data ,, then incase a validation error occurs it will not be displayed on the view.
raduenuca
All-Star
24675 Points
4250 Posts
Re: Can i return a Partial view and a Json object from my action method at the same time
May 03, 2012 02:58 PM|LINK
Please explain what are you trying to do. Do you want to call tha action with an AJAX request and return Json and in case the javascript is disabled return the view?
If not then do as Andrei said and add that property as a hidden field in the partial view. You can read it then from JavaScript and decide what to do.
Radu Enuca | Blog
bruce (sqlwo...
All-Star
36644 Points
5432 Posts
Re: Can i return a Partial view and a Json object from my action method at the same time
May 03, 2012 03:12 PM|LINK
http only allows 1 response per request. your view should have a hidden field that javascript queries after the reponse to check success.
johnjohn1231...
Participant
922 Points
871 Posts
Re: Can i return a Partial view and a Json object from my action method at the same time
May 03, 2012 06:06 PM|LINK
wht i am trying to do as follow:-
1. i am calling the action method using ajax.beginform
using (Ajax.BeginForm("CreateAll", "VisitLabResult", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = item.ToString(), InsertionMode = InsertionMode.Replace, OnSuccess = string.Format( "disableform({0})", Json.Encode(item)), }))2. after succfully receiving the response from the server ,, the Onsuccess script will be executed,,, the script simply disable the form:-
function disableform(id) { $('#' + id + ' :input').prop("disabled", true); }The problme is that the script will always disable the form even is some validation error occurs,, so what i was trying to achieve is to return a JSON with the partial view that indicate if the ModelState.IsValid was valid or not ,, and if it was not valid to keep the form enabled to allow the user to correct the validation errors.
BR