简体   繁体   中英

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. But the browser does not render the response. I've verified the response is good in 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.

I want to use the FormCollection. Why doesn't the browser render the response and what can I do to fix it?

BoMController

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

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.

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:

$('#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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