简体   繁体   English

Ajax调用Controller后的ASP.NET MVC Show View

[英]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.

相关问题 如果Controller失败,如何在View中显示错误? ASP.NET MVC - How to show error in View if the Controller fails ? ASP.NET MVC 在没有ajax调用的asp.net mvc控制器中获取html表 - get html table in asp.net mvc controller without ajax call 如何从 AJAX 方法返回列表以从 controller ASP.NET MVC 查看 - How to return a list from AJAX method to view from controller ASP.NET MVC ASP.Net Mvc 如何使用 Ajax 使用按钮将数据从视图传递到 Controller - ASP.Net Mvc How to pass data from View into Controller with a Button using Ajax 当我从 ASP.NET MVC 中的 controller 返回数据时如何调用模态 - How to call a modal when I return data a partial view from the controller in ASP.NET MVC 如何使用 ASP.NET MVC C# Html 从视图中调用控制器中的函数 - How to call a function in the controller from the view using ASP.NET MVC C# Html 在 ASP.NET MVC 中实施 AJAX 结果后,如何在 html 部分显示 ViewBag 消息? - How can I show ViewBag message in html part after implementing AJAX result in ASP.NET MVC? 从Controller传递Html字符串以查看ASP.Net MVC - Pass Html String from Controller to View ASP.Net MVC Asp.net MVC 5 - 查看向控制器发布空数据 - Asp.net MVC 5 - View Posting Null Data to Controller "ASP.NET MVC 将原始 HTML 从控制器传递到视图" - ASP.NET MVC Passing Raw HTML from Controller to View
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM