开发者

JQuery, Call Action and return View

开发者 https://www.devze.com 2023-04-13 08:56 出处:网络
I\'have a dialog box in JQuery that have two buttons. When the button \"Upload Anyway\" is clicked I call an Action of my Controller.

I'have a dialog box in JQuery that have two buttons. When the button "Upload Anyway" is clicked I call an Action of my Controller. $(function () { $("#dialog:ui-dialog").dialog("destroy");

$("#dialog-confirm").dialog({
    resizable: false,
    height: 300,
    width: 500,
    modal: true,
    buttons: {
        "Upload Anyway": function () {
            $(this).dialog("close");
   开发者_如何学C         var month = '@ViewBag.duplicateString' ;
            var path = $("#path").val();
           $.getJSON('@Url.Action("UpdateComp")', { dateToUpdate: month, filePath: path }, 
            function()   {});
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    }
});

In the Controller I just want to process the data and return a view.

    public ActionResult UpdateComp (string dateToUpdate, string filePath)
    {
        //Process Data
        return View(compList.Values.AsEnumerable<CompUser>());
    }

When I click over the "Upload Anyway" I'm correctly redirected to the Action (I've checked with debug), but the view is not loaded. I'm not very good at JQuery so maybe I'm doing something wrong.

Thanks for your help!


Your callback on $.getJSON is doing nothing. See http://api.jquery.com/jQuery.getJSON/ and look at the "success" parameter.

But, if you're returning a View (HTML, etc), then you're not "getting JSON", you're getting HTML. Try using $.get or $.post (depending on requirement) instead and displaying the returned view to the user in your success callback.


Changing the $.getJSON by $.get and display the resulted view in my main div solve the problem.

Note that I render a partial view in order to display the div correctly.

Here is the JQuery code, hope that will help some other person.

$(function () {
$("#dialog:ui-dialog").dialog("destroy");
$("#dialog-confirm").dialog({
    resizable: false,
    height: 300,
    width: 500,
    modal: true,
    buttons: {
        "Upload Anyway": function () {
            $(this).dialog("close");
            var month = '@ViewBag.duplicateString' ;
            var path = $("#path").val();
            $.get('@Url.Action("UpdateComp")', { dateToUpdate: month, filePath: path }, function(result) {
                $('#main_div').html(result);
            })
                .error(function() { alert("A Problem occurs during the comparison"); });
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    }
  });
});


Try this

var url = '@Url.Action("method_name", "controller_name")?dateToUpdate=' + month + '&filePath=' + path;
window.location.href = url;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号