[英]ASP.NET MVC Show View after Ajax call to a Controller
I have a View with a submit form: when I click it a jquery/ajax function is called. 我有一个提交表单的视图:当我点击它时,调用一个jquery / ajax函数。 This function has to encode the View Model, call a Controller action and show the View returned.
此函数必须对View Model进行编码,调用Controller操作并显示返回的View。 Now, this is my function:
现在,这是我的功能:
<script type="text/javascript">
function Analyze() {
var urlact = '@Url.Action("Analysis")';
var model = '@Html.Raw(Json.Encode(Model))';
$.ajax({
data: model,
type: "POST",
url: urlact,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
//WHAT HERE??
}
});
}
</script>
And the Analysis action is a kind of 分析动作是一种
public ViewResult Analysis(IEnumerable<Azienda> aziende) {
Debug.WriteLine(aziende.Count());
return View(aziende);
}
Returning a View! 回到视野! How can I display that View on success:function(data)?
如何在成功时显示该视图:功能(数据)? I tried changing dataType to html and calling on success alert(data) but I had problems with the encoded model, I tried commenting the contentType line but same model-encoding issue.
我尝试将dataType更改为html并调用成功警报(数据),但我遇到编码模型的问题,我尝试评论contentType行,但模型编码问题相同。
Does someone know how to do that? 有人知道怎么做吗? A js/query/ajax workaround-trick is fine, too.
一个js / query / ajax的解决方法也很好。
Thanks you all! 谢谢大家!
Use 采用
return PartialView()
instead of 代替
return View()
in your controller. 在你的控制器中。 Then in the success function in your ajax call, use the jQuery .html() function to update the element you wish to update in your html.
然后在你的ajax调用的success函数中,使用jQuery .html()函数更新你想在html中更新的元素。 See Query.html() and View() vs. PartialView()
请参阅Query.html()和View()与PartialView()
Create a separate partial view with aziende as its model and return this in your Analysis action and then append the result to a div in your view: 使用aziende作为模型创建单独的局部视图,并在Analysis操作中返回此结果,然后将结果追加到视图中的div:
//action returning a partial view
public ActionResult Analysis(IEnumerable<Azienda> aziende)
{
Debug.WriteLine(aziende.Count());
return PartialView("_partialView", aziende);
}
//then append the result to a div in your javascript
success: function (data) {
$("#some-div").html(data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.