Ajax.BeginForm that can redirect to a new page

后端 未结 1 325
半阙折子戏
半阙折子戏 2020-12-05 18:05

I have an @Ajax.BeginForm for my model which has a boolean value (@Html.CheckBoxFor). If this is checked, I want my HttpPost action to redirect to

相关标签:
1条回答
  • 2020-12-05 18:33

    You could use JSON and perform the redirect on the client:

    [HttpPost]
    public ActionResult UpdateModel(BasketModel model)
    {
        if (model.Checkout)
        {
            // return to the client the url to redirect to
            return Json(new { url = Url.Action("Checkout") });
        }
        else
        {
            return PartialView("_Updated");
        }
    }
    

    and then:

    @using (Ajax.BeginForm("UpdateModel", "MyController", new AjaxOptions { OnSuccess = "onSuccess", UpdateTargetId = "foo" }))
    {
        ...
    }
    

    and finally:

    var onSuccess = function(result) {
        if (result.url) {
            // if the server returned a JSON object containing an url 
            // property we redirect the browser to that url
            window.location.href = result.url;
        }
    }
    
    0 讨论(0)
提交回复
热议问题