微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – ASP.net MVC Controller不会返回视图

对ASP.net来说还是个新手,我有这个奇怪的问题.这是一个非常基本的场景,但有些东西已经出现,我无法弄明白.部署应返回名为Deploy的视图,该视图将键入到模型CompiledAppModel中.但是,当您在视图中单击“安装”时,尽管调用了返回的View()方法,它仍然不会离开页面.有任何想法吗?

这是我的控制器:

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

在你的控制器中,只需:

[HttpPost]
public ActionResult Deploy(string key_name,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,custom_folder);
    // then we'll call to generate client side info
    bool clientInstall = _fms_app_service.CompileClientApp(user_info,model);
    }
    return AjaxAwareRedirectResult("/foo");
}

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

原文地址:https://www.jb51.cc/csharp/100425.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐