c# - ASP.net MVC Controller 不会返回 View

标签 c# asp.net-mvc

对 ASP.net 还是有点陌生​​,我遇到了这个奇怪的问题。这是一个非常基本的场景,但出现了问题,我无法弄清楚。 Deploy 应该返回一个名为 Deploy 的 View ,该 View 被键入到模型 CompiledAppModel 中。但是,当您在 View 中单击安装时,尽管调用了 return View() 方法,但它永远不会离开页面。有什么想法吗?

这是我的 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");
}

我的观点:

@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>

还有模型。

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; }
}

最佳答案

我假设您真的想在进行 ajax 调用后进行重定向。

据我所知,您必须实现自定义 ActionResult,例如:

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);
        }
    }
}

在您的 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 AjaxAwareRedirectResult("/foo");
}

但是,正如我所说,这只是我假设您真的想在 ajax 调用后重定向。

关于c# - ASP.net MVC Controller 不会返回 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12906868/

相关文章:

c# - 在 Windows 应用商店应用程序中使用 hmacsha256

c# - 无法以编程方式安装 OOB SL5

c# - Collider2D 在不同的游戏对象上触发 C# Unity3D 不工作

c# - List<System.Type> 是否只接受某些类型?

asp.net-mvc - ASP.NET MVC : What is the purpose of @section?

c# - 文本框和线程

c# - 使用 Entity Framework 从 ASP.NET MVC (C#) 中的 DropDown 获取 SelectedValue

javascript - 无法通过 javascript 链接回 MVC View

asp.net - 我如何从进程中慢慢迁移到使用 Redis 作为 session 状态提供程序?

asp.net-mvc - 哪些企业站点正在使用 ASP.NET MVC/MVC 2?需要管理转向 MVC 的理由