控制器返回原始JSON对象视图

问题描述:

我使用MVC架构,我有一个POST形式,引导模态是重视它的表单数据后提交给AJAX调用控制器返回原始JSON对象视图

$.ajax({ 
    type: "POST", 
    url: action, 
    enctype: "multipart/form-data", 
    data: dataString, 
    cache: false, 
    contentType: contentType, 
    processData: processData, 
    beforeSend: function() { 
     block('#mymodal'); 
    }, 
    success: function (data, status, xhr) { console.log("success ajax"); onAjaxSuccess(xhr, status, '#mymodal') }, 
    error: function (xhr, status) { console.log("error ajax"); onAjaxFailed(xhr, status, error, '#error-div') }, 
    complete: function (xhr, status) { console.log("complete ajax"); onAjaxComplete(xhr, status, '#mymodal', '#alert', '#myModal') } 
}); 

其中行动是控制器的方法,其中它把所需的数据,的contentType过程数据均为假

此AJAX调用工作正常和正确地发送呼叫到控制器

public ActionResult MyCtroller(myViewModel model) 
{ 
    //processing stuff 

    JsonResultObject result = new JsonResultObject(); 

    try 
    { 
     //return secess 
    } 
    catch (Exception ex) 
    { 
     //return error 
    } 

    SearchCriteria<MyModel> viewModel = new SearchCriteria<MyModel>(); 
    viewModel.SortExpression = m => m.OrderByDescending(a => a.Date); 
    SearchResult<MyModel> searchResult = MyModelService.Instance.Search(viewModel); 

    result.PartialViewHtml = RenderPartialViewToString("PartialView.cshtml", searchResult); 


    return Json(result)); 
} 

完成后的处理,它的时间返回的页面就打印出结果作为纯粹的JSON与该JSON作为对象的局部视图,而不是渲染partialView和成功,完整,错误在Ajax调用前面不会被调用

{ 
    "IsRedirect": false, 
    "RedirectUrl": null, 
    "Success": true, 
    "AlertMessage": { 
    "IsAutoHide": false, 
    "Dissmisable": true, 
    "ShowIcon": false, 
    "Message": "success", 
    "AlertCSS": "alert alert-success", 
    "AlertType": 3, 
    "AlertTypeMetronic": "success" 
}, 
"PartialViewHtml":"-----partialView HTML code-----" 
} 

你应该直接跟你打算序列化数据调用JsonJson调用将返回JsonResult对象,因此不要将其传递给JsonResult的实例。如果您确实想直接使用JsonResult,则无需额外致电Json即可返回。

同时使用JsonJsonRequestBehavior参数的过载。

[HttpPost] 
public ActionResult MyCtroller(myViewModel model) 
{ 
    var result = new ActualInstanceOrContainerToBeReturned; 
    return Json(result, JsonRequestBehavior.AllowGet); 
} 

而且我不知道你为什么会想返回JsonResult里面的观点,所以我不会只是说可能是糟糕的设计发表评论。为了使SOC保持数据和视图分离(这包括这些项目的生成)。

我认为你需要改变你的控制器

public ActionResult MyCtroller(myViewModel model) 
{ 
    //processing stuff 

    JsonResultObject result = new JsonResultObject(); 

    try 
    { 
     //return secess 
    } 
    catch (Exception ex) 
    { 
     //return error 
    } 

    SearchCriteria<MyModel> viewModel = new SearchCriteria<MyModel>(); 
    viewModel.SortExpression = m => m.OrderByDescending(a => a.Date); 
    SearchResult<MyModel> searchResult = MyModelService.Instance.Search(viewModel); 

    // result.PartialViewHtml = RenderPartialViewToString("PartialView.cshtml", searchResult); 

    // If you want to render as html partial view 

    return PartialView("PartialView.cshtml", searchResult); 

    // return Json(result)); 
} 

和Javascript代码

$.ajax({ 
      type: "POST", 
      url: action, 
      enctype: "multipart/form-data", 
      data: dataString, 
      cache: false, 
      contentType: contentType, 
      processData: processData, 
      beforeSend: function() { 
       block('#mymodal'); 
      }, 
      success: function (data, status, xhr) { 
        console.log("success ajax"); 
        onAjaxSuccess(xhr, status, '#mymodal') 
        $("#YOUR_DIV_ID").html(data); 
      }, 
      error: function (xhr, status) { console.log("error ajax"); onAjaxFailed(xhr, status, error, '#error-div') }, 
      complete: function (xhr, status) { console.log("complete ajax"); onAjaxComplete(xhr, status, '#mymodal', '#alert', '#myModal') } 
     });