2016-12-07 61 views
0

我有局部視圖後失去了在那裏我做了提交:MVC:其他部分意見後,以局部視圖

<input type="image" src="~/Resources/Images/DoSomething.png" border="0" alt="Submit" value="DoSomething" name="DoSomething"/> 

控制器功能:

[HttpPost] 
[MultipleButton] 
public ActionResult DoSomething(MyModel myModel) 
{ 
    DoSomethingToManipulateTheModel(myModel); 

    return PartialView("~/Views/MyPartialView.cshtml", myModel); 
} 

在功能DoSomething的( )我想操縱myModel並將其發送回視圖。

但我有一個問題:
調用DoSomething後,只顯示局部視圖,沒有樣式。 如何在不丟失其他部分視圖和樣式的情況下更新partialview?

+0

除非發生這種情況通過某些AJAX構建,你基本上加載僅包含由服務器返回的局部視圖了新的一頁。要麼返回整個頁面,要麼以某種方式使用AJAX來只更新瀏覽器頁面的一部分,而不是進行表單發佈。 – David

回答

0

你可以用$.ajax做到這一點,添加onclick="someJsFunction()"到您的按鈕,並在

someJsFunction(e) 
{ 
e.preventDefault(); 

//do the ajax hit here to your action method 
//and on the success event of ajax append the partial view html to the //corresponding div where you want to show the partial content 

//and don't forget to set the datatype:html in ajax parameters 

} 
0

您有以下代碼,用來局部視圖的返回結果

<input type="image" src="~/Resources/Images/DoSomething.png" border="0" id="SubmitBtn" alt="Submit" value="DoSomething" name="DoSomething"/> 

您必須添加「 PartialResultDivName「Div在您的視圖上並顯示該div中的返回數據。檢查下面的代碼。

$("#SubmitBtn").click(function() { 
 
    $.ajax(
 
        { 
 
         url: '@Url.Action("DoSomething", "ControllerName")', 
 
         type: "Post", 
 
         async: false, 
 
         dataType: "html", 
 
         contentType: "application/json;charset=utf-8", 
 
         data: $('form').serialize(), //here you have to pass your form name for pass all data to controller action 
 
        contentType: "application/json;charset=utf-8", 
 
         success: function (data) { 
 
          $('#PartialResultDivName').html(data); 
 
         } 
 
        }); 
 
});

+0

你說過:「你必須在你的視圖中添加」PartialResultDivName「Div。我不知道該把它放在哪裏。我用我的主div的ID替換了PartialResultDivName,但這不起作用 – EricH

+0

它的div的ID顯示返回結果。在該視圖上添加了該主視圖上的按鈕。 –

+0

然後我做了你的建議。但結果是一樣的。僅顯示局部視圖 – EricH