ASP.NET MVC Controller will not return view

There is still something new for ASP.net, and I have this strange problem. This is a very simple scenario, but something is wrong, and I cannot figure it out. The deployment should return a view named Deploy, which is entered into the CompiledAppModel model. However, when you click on the installation in the view, it never leaves the page, despite the call to the return () method. Any ideas?

Here is my controller:

[HttpPost] public ActionResult Deploy(string key_name, string custom_folder = "") { string userId = Membership.GetUser().ProviderUserKey.ToString(); UserDataModel user_info = _user_data_service.getUserDataByPrimaryIDNoDB(userId, HttpContext.Cache); log.Trace("Deploy was called. key_name:" + key_name + " UID: " + user_info.UID); // first we'll call the info to install remote application bool serviceInstall = _fms_app_service.DeployFmsApp(user_info, key_name, custom_folder); // then we'll call to generate client side info bool clientInstall = _fms_app_service.CompileClientApp(user_info, key_name); var model = _fms_app_service.getInstalledAppInfo(user_info, key_name); if (serviceInstall && clientInstall) { return RedirectToAction("Deploy", model); } return View("Error"); } 

and my opinion:

 @model IEnumerable<Models.Applications.FmsAppModel> @foreach (var item in Model) { <div class="col"> <h2>@Html.DisplayFor(modelItem => item.friendly_name)</h2> <p>@Html.DisplayFor(modelItem => item.app_description)</p> <p><strong>Tags:</strong> @Html.DisplayFor(modelItem => item.app_type)</p> <a href="#" class="btn btn-primary install-app" data-key-name="@(item.key_name)">Install</a> @Html.ActionLink("Details", "Detailss", new { id=item.app_id }) </div> } </div> <script type="text/javascript"> (function () { $('.install-app').on('click', function (e) { e.preventDefault(); var data_key_name = $(this).data('key-name'); //ajax install app $.ajax({ type: "POST", url: '@Url.Action("Deploy")', data: { key_name: data_key_name } }); }); })(); </script> 

And the model.

 public class CompiledAppModel { [Display(Name = "Admin URL")] public string adminURL { get; set; } [Display(Name = "Viewer URL")] public string viewerURL { get; set; } [Display(Name = "Embed URL")] public string embedURL { get; set; } } 
+7
source share
3 answers

I assume that you really want to redirect after ajax call.

As far as I know, you should implement a custom ActionResult, for example:

 public class AjaxAwareRedirectResult : RedirectResult { public AjaxAwareRedirectResult(String url) : base(url) { } public override void ExecuteResult(ControllerContext context) { if ( context.RequestContext.HttpContext.Request.IsAjaxRequest() ) { String destinationUrl = UrlHelper.GenerateContentUrl(Url, context.HttpContext); JavaScriptResult result = new JavaScriptResult() { Script = "window.location='" + destinationUrl + "';" }; result.ExecuteResult(context); } else { base.ExecuteResult(context); } } } 

In your controller, simply do:

  [HttpPost] public ActionResult Deploy(string key_name, string custom_folder = "") { string userId = Membership.GetUser().ProviderUserKey.ToString(); UserDataModel user_info = _user_data_service.getUserDataByPrimaryIDNoDB(userId, HttpContext.Cache); log.Trace("Deploy was called. key_name:" + key_name + " UID: " + user_info.UID); // first we'll call the info to install remote application bool serviceInstall = _fms_app_service.DeployFmsApp(user_info, key_name, custom_folder); // then we'll call to generate client side info bool clientInstall = _fms_app_service.CompileClientApp(user_info, key_name); var model = _fms_app_service.getInstalledAppInfo(user_info, key_name); if (serviceInstall && clientInstall) { return RedirectToAction("Deploy", model); } return AjaxAwareRedirectResult("/foo"); } 

But, as I said, this is just me assuming that you really want to redirect after ajax call.

+4
source

It seems to me that you are using an Ajax call to send a message to the server, in which case the result will not be displayed on the page without additional work. You can define an ajax call success handler to make a decision when returning an ajax call. So something like

 <script type="text/javascript"> (function () { $('.install-app').on('click', function (e) { e.preventDefault(); var data_key_name = $(this).data('key-name'); //ajax install app $.ajax({ type: "POST", url: '@Url.Action("Deploy")', data: { key_name: data_key_name }, success: function(data) { alert(data) } }); }); })(); 

will provide you with an HTML message message returned from an ajax call when the request is complete.

I also used Firebug or the developer tools in Chrome to view the returned HTML, if an exception is thrown on the server, you can examine this in more detail using these tools.

+1
source

Performing an HTTP redirect after an ajax call might not make much sense. You can do this simply with an html form and a regular POST.

However, if you really need to redirect and still want POST via AJAX, you can do this by returning Json instead of redirecting, as suggested in the following answers:

In this case, you can return the URL of the target action inside the Json object and use this information in your success handler, as shown in Neil's answer.

Also, remember that RedirectToAction is just a 302 redirect, so it won’t display the view directly, so you won’t give it a full-blown ViewModel, just the action url for the browser to issue a GET to.

This target GET action should be responsible for receiving your installed application information and passing this parameter in the form following the POST-REDIRECT-GET template .

+1
source

All Articles