0

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" }, with the Partial view.

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

::-UPDATED-::

what 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 successfully 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 problem 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

Iridio
  • 9,213
  • 4
  • 49
  • 71
John John
  • 1
  • 72
  • 238
  • 501

4 Answers4

0

You can return ONLY ONE view from action method, if at all you want to pass other information,make use of ViewData or ViewBag

ViewBag.IsSuccess =  "True";

Or

ViewData["IsSuccess"] = "True";
Pravin Pawar
  • 2,559
  • 3
  • 34
  • 40
0

No, you can return only the view and pass JSON as the model, or ViewBag (I recommend model.)

bobek
  • 8,003
  • 8
  • 39
  • 75
  • I think that neither the viewbag nor the viewmodel will solve my problem, PLEASE CHECK MY UPDATE TO SEE MORE DETAILS OF THE PROBLEM I AM FACING – John John May 03 '12 at 20:56
0

Why not simply extend the model that you are already passing to the View adding the property IsSuccess?

ViewBag or ViewData are evil in my opinion. Try to always use a ViewModel when returning data to the view.

dplante
  • 2,445
  • 3
  • 21
  • 27
Iridio
  • 9,213
  • 4
  • 49
  • 71
0

In such cases I used following solution:

In your ajax form definition set:

OnComplete = "yourCallback"

Then:

yourCallback = function(response){
  var json = response.responseJSON;
  if(json.success){
     alert('Well done!');
  } else {
     var form = $('#formId');
     form.html(json.html);
     form.removeData("validator").removeData("unobtrusiveValidation");
     $.validator.unobtrusive.parse(form);
  }
}

Your controller should return something like this:

var result = new { success = false, html = helper.Partial("_YourPartial", model) };
return Json(result);

Where helper helps you to add validation to your partial view. (Described here: https://stackoverflow.com/a/4270511/952023)

Community
  • 1
  • 1
Pj_pavel
  • 387
  • 3
  • 16