2016-12-07 42 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