2013-10-23 30 views
0

我有以下代碼發佈到我的搜索Json。問題是url重定向到json搜索並顯示原始json數據。我想返回到我的partialView中的表格。有關我如何實現這一目標的任何想法?如何從部分視圖FormMethod.Get返回JSON數據?

<div> 
@using (Html.BeginForm("Search", "Home", Formmethod.Get, new {id="search-form"})){ 
    ... 
    <button id="search-btn">Search</button> 
} 
</div> 
<div> 
    <table id="search-results">...</table> 
</div> 

我家的控制器工作正常,但以確保畫面清晰...

public JsonResult Search(/*variables*/) 
{ 
    ... 
    return Json(response, JsonRequestBehavior.AllowGet); 
} 

而且我重定向到「搜索/(我的所有變量)

回答

0

您的響應數據應放入模型中,模型傳遞到局部視圖,並從控制器返回局部視圖。

public PartialViewResult Search(/*variables*/) 
{ 
    ... 
    YourModel model = new YourModel(); 
    // Populate model 
    return PartialView("_YourPartialView", model); 
} 

如果只想刷新局部視圖,則可以通過ajax調用控制器操作,並在ajax回調中調用$('#yourDivContainingThePartialView').html(response)

$.ajax({ 
    url: urlToYourControllerAction, 
    method: 'get', // or 'post', depending on whether your controller action has side effects 
    success: function (response) { 
     $('#yourDivContainingThePartialView').html(response); 
    } 
    // other ajax options here if you need them 
});