How to asynchronously call multiple stored procs(model) and assign the result set from controller to a variable in view in MVC? [closed]

StackOverflow https://stackoverflow.com/questions/20739418

  •  20-09-2022
  •  | 
  •  

質問

I use edmx in the model.

Now in the controller I understand that Index will be called initially.

What if i want to fetch multiple result sets on pageload?? how can i call multiple stored procs and assign results from them to variables in the view??

Controller code

public ActionResult ViewQuery() {

  DBController dBController = new DBController();
  ViewBag.JsonData = new JavaScriptSerializer().Serialize(dBController.xxxxx());
  return View();
  }

Now in the view i have the below line, which fetches value from the controller if the code is placed in Index()

 var resultset=@(Html.Raw(ViewBag.JsonData))

If I have the code that is currently in ViewQuery() inside Index() function, then it is working fine, but if i have it in ViewQuery it is not working !! I am not getting the ViewBag.JsonData Please help

役に立ちましたか?

解決 2

Improve perceived performance of ASP.NET MVC websites with asynchronous partial views
This post tell you how to use jquery and mvc parital views to load page asynchronously.

If you don't want to use this way, please check this question How to send JsonResult from a Controller to a View and access it using jQuery?

Updated(Change your code):

public ActionResult ViewQuery() 
{       
    DBController dBController = new DBController();     
    ViewBag.JsonData= dBController.GetXXX(); 
    return Json(dBController.usp_CIOChallenge_Admin_view_query(), JsonRequestBehavior.AllowGet);
}

in your view you can make ajax call to the controller to fetch results

$.ajax({
    url: "Home/ViewQuery",
    responseType:"json",
    success: successfn,
    error:errorfn
});
function successfn(result) {

};

他のヒント

Need add JsonRequestBehavior.AllowGet in Json(...)

   public ActionResult ViewQuery() 
   {   
       DBController dBController = new DBController();
       var result = Json(dBController.usp_xxxxxxx());
       return Json(dBController.usp_xxxxxxx(),JsonRequestBehavior.AllowGet);
   }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top