简体   繁体   English

浏览器为什么不呈现控制器的响应?

[英]Why doesn't the browser render a response from the controller?

When an POST is made to the controller, the controller responds with exactly what I want the browser to render. 对控制器进行POST时,控制器将以我希望浏览器呈现的内容完全响应。 But the browser does not render the response. 但是浏览器不会呈现响应。 I've verified the response is good in Fiddler. 我已经验证了Fiddler的响应良好。

The code below shows what I think is relavent code. 下面的代码显示了我认为是相关的代码。 The controller action method that returns the response, part of the template that has the mvc helper code, javascript/jquery code that fires the ajax call with the form inputs. 返回响应的控制器操作方法,该模板是具有mvc帮助程序代码的模板的一部分,该javascript / jquery代码使用表单输入触发ajax调用。

I want to use the FormCollection. 我想使用FormCollection。 Why doesn't the browser render the response and what can I do to fix it? 浏览器为什么不呈现响应,我应该怎么做才能解决它?

BoMController BoMController

public ActionResult GetBillOfMaterialsView(FormCollection frmColl){
    // snipped out model interaction
    return PartialView("~/Views/Project/Index.cshtml", project);
}

Index.cshtml Index.cshtml

@using (Html.BeginForm("GetBillOfMaterialsView", "BoM", FormMethod.Post, new {id = "frmGetBom"})) { 
     // selProj input select code removed for brevity 
}

    function submitGetBoM() {
        var frmGetBom = $('#frmGetBom');
        $.ajax({
            type: 'POST',
            url: frmGetBom.attr('action'),
            data: frmGetBom.serialize()
        });
    }

    $(document).ready(function() {
        $('#selProj').selectmenu( {
            select: function(){submitGetBoM()}
        }).addClass("overflow");
    });

Invoking $.ajax alone doesn't append the response from the server to the document, you have to use the success callback to manually fetch the response and append it. 单独调用$.ajax并不能将服务器的响应追加到文档中,您必须使用success回调函数来手动获取并追加响应。

For example: 例如:

$.ajax({
    type: 'POST',
    url: frmGetBom.attr('action'),
    data: frmGetBom.serialize(),
    success: function(response) {
         $('#someContainerId').html(response);
    }
});

Alternatively, use load() that is a shorthand to the above: 或者,使用上述的简写形式的load()

$('#someContainerId').load(frmGetBom.attr('action'), frmGetBom.serializeArray());

See Documentation 请参阅说明文件

Your client code doesn't do anything with the returned values from the server: 您的客户端代码对服务器返回的值不做任何事情:

function submitGetBoM() {
    var frmGetBom = $('#frmGetBom');
    $.ajax({
        type: 'POST',
        url: frmGetBom.attr('action'),
        data: frmGetBom.serialize(),
        success: function() { alert('ok'); }
    });
}

This would popup an alert on success. 这将弹出有关成功的警报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM