Can i return a Partial view and a Json object from my action method at the same timehttp://forums.asp.net/t/1799445.aspx/1?Can+i+return+a+Partial+view+and+a+Json+object+from+my+action+method+at+the+same+timeThu, 03 May 2012 18:06:30 -040017994454962719http://forums.asp.net/p/1799445/4962719.aspx/1?Can+i+return+a+Partial+view+and+a+Json+object+from+my+action+method+at+the+same+timeCan i return a Partial view and a Json object from my action method at the same time <p>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 = &quot;True&quot; }, in addtion to the Partial view. Part of my Action method looks as follow:-</p> <pre class="prettyprint">try { if (ModelState.IsValid) { var v = repository.GetVisit(visitid); if (!(v.EligableToStart(User.Identity.Name))) { return View(&quot;NotFound&quot;); } vlr.VisitID = visitid; repository.AddVisitLabResult(vlr); repository.Save(); ViewBag.LabTestID = new SelectList(repository.FindAllLabTest(), &quot;LabTestID&quot;, &quot;Description&quot;, vlr.LabTestID); // return Json(new { IsSuccess = &quot;True&quot; }, JsonRequestBehavior.AllowGet); @ViewBag.status = &quot;Added Succsfully&quot;; return PartialView(&quot;_create&quot;,vlr) ; } }</pre> <p><br> <br> <br> BR</p> <p></p> 2012-05-02T23:54:08-04:004962836http://forums.asp.net/p/1799445/4962836.aspx/1?Re+Can+i+return+a+Partial+view+and+a+Json+object+from+my+action+method+at+the+same+timeRe: Can i return a Partial view and a Json object from my action method at the same time <p></p> <blockquote><span class="icon-blockquote"></span> <h4>johnjohn123123</h4> Can i return a Partial view and a Json object from my action method at the same time</blockquote> <p></p> <p>Return only the data and isSuccess . The html part will be on the client side. Please see http://bit.ly/mvc_ajax_jquery</p> <p></p> 2012-05-03T03:21:13-04:004963929http://forums.asp.net/p/1799445/4963929.aspx/1?Re+Can+i+return+a+Partial+view+and+a+Json+object+from+my+action+method+at+the+same+timeRe: Can i return a Partial view and a Json object from my action method at the same time <p>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.</p> 2012-05-03T14:12:45-04:004964008http://forums.asp.net/p/1799445/4964008.aspx/1?Re+Can+i+return+a+Partial+view+and+a+Json+object+from+my+action+method+at+the+same+timeRe: Can i return a Partial view and a Json object from my action method at the same time <p>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?</p> <p>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.</p> <p>&nbsp;</p> 2012-05-03T14:58:21-04:004964029http://forums.asp.net/p/1799445/4964029.aspx/1?Re+Can+i+return+a+Partial+view+and+a+Json+object+from+my+action+method+at+the+same+timeRe: Can i return a Partial view and a Json object from my action method at the same time <p>http only allows 1 response per request. your view should have a hidden field that javascript queries after the reponse to check success.</p> 2012-05-03T15:12:39-04:004964296http://forums.asp.net/p/1799445/4964296.aspx/1?Re+Can+i+return+a+Partial+view+and+a+Json+object+from+my+action+method+at+the+same+timeRe: Can i return a Partial view and a Json object from my action method at the same time <p>wht i am trying to do as follow:-</p> <p>1. i am calling the action method using ajax.beginform</p> <pre class="prettyprint">using (Ajax.BeginForm(&quot;CreateAll&quot;, &quot;VisitLabResult&quot;, new AjaxOptions { HttpMethod = &quot;Post&quot;, UpdateTargetId = item.ToString(), InsertionMode = InsertionMode.Replace, OnSuccess = string.Format( &quot;disableform({0})&quot;, Json.Encode(item)), }))</pre> <p>2. after succfully receiving the response from the server ,, the Onsuccess script will be executed,,, the script simply disable the form:-</p> <pre class="prettyprint">function disableform(id) { &#36;('#' + id + ' :input').prop("disabled", true); }</pre> <p><strong><span style="text-decoration:underline">The problme is that the script will always disable the form even is some validation error occurs,,</span> </strong>so what i was trying to achieve is to return a JSON with the partial view that indicate if the <span class="pun"></span><span class="typ">ModelState</span><span class="pun">.</span><span class="typ">IsValid</span> was valid or not ,, <strong><span style="text-decoration:underline">and if it was not valid to keep the form enabled to allow the user to correct the validation errors.</span></strong></p> <p>BR<br> <br> <br> </p> <p></p> 2012-05-03T18:06:30-04:00