首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC弹出和PostBack

MVC弹出和PostBack
EN

Stack Overflow用户
提问于 2020-02-25 10:22:12
回答 1查看 1.2K关注 0票数 0

我有一个MVC c#,signalR项目,其中代理遵循应用程序中的以下步骤

  • 登录到应用程序。一旦登录成功,应用程序隐藏login面板&显示活动和电话按钮列表,

  • 应用程序显示活动代理列表分配给每个活动前面的
  • 应用程序显示按钮,以在活动中设置就绪/未就绪。在这种情况下,是Telemarketing
  • If & RestAPI代理需要设置自己,在竞选中没有做好准备,它打开弹出窗口,并列出未准备好的原因。问题是:当代理选择原因并提交应用程序时,
  • 会回发失去的视图,并重置到登录窗口。

在PopUp窗口中提交破坏原因后的控制器操作:

代码语言:javascript
复制
 public ActionResult SetBreak(breakReasonModel form)
    {
        string tok=form.accessToken;
        string cmp = form.campaign;
        string selreason = "";
        for (int i=0;i < form.arrReasons.Length;i++)
        {
             selreason = form.arrReasons[i];
        }
        SetBreak obj = new SetBreak();
        System.Collections.Generic.List<ISCampaigns> IScampaignNames = new System.Collections.Generic.List<ISCampaigns>();
        IScampaignNames = obj.setNotReadyInCampaign(tok, cmp, selreason);

        return RedirectToAction("Index");
    }

PopUp部分视图:

代码语言:javascript
复制
 @using Altitude.IntegrationServer.RestApiWebApp.Models
@model Altitude.IntegrationServer.RestApiWebApp.Models.breakReasonModel

<div id="divBreakReasons">
    @using (Html.BeginForm("SetBreak", "Home"))
    {

        @Html.ListBoxFor(m => m.arrReasons, Model.reasonsMultiSelectList, new { @class = "form-control" })

        @Html.TextBoxFor(model => model.accessToken, new { id = "txtaccessToken" })

        @Html.TextBoxFor(model => model.campaign, new { id = "txtcampaign" })
        <br />
        <button id="btn" type="submit" class="btn btn-block bg-primary" value="Submit" >Submit</button>
        <br />

    }
</div>

Index.chtml

代码语言:javascript
复制
<div class="row">
    <div class="col-md-4 table-responsive" id="telButtons">
        <table id="tblTelephony" class="table">
           --Telephony Buttons
            </tbody>
        </table>
    </div>
</div>
<div class="row">
    <div class="col-md-4 table-responsive">
        <p id="demo"></p>  // Campaign table with Ready/Not Ready buttons
    </div>
</div>
//ajax call to open popup
<div id="dialog" style="display: none"></div>
<script type="text/javascript">
    function getBreak(nrReason) {
        $("#dialog").dialog({
            autoOpen: false,
            modal: true,
        }); 
    $.ajax({
        type: "POST",
        url: "@Url.Action("popupBreak","Home")",
        data: '{breakReason : "' + dataToSend + '",accessToken : "' +acc+ '",campaign : "' + cmp + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "html",
        success: function (response) {
            $('#dialog').html(response);
            $('#dialog').dialog('open');
            console.log(response);
        },
        failure: function (response) {       
        },
        error: function (response) {       
        }
        });
    }
</script>
EN

回答 1

Stack Overflow用户

发布于 2020-02-25 15:39:44

就像你编码的那样。如果需要将结果返回到当前视图,则应该使用ajax调用来返回操作结果。

示例

代码语言:javascript
复制
@using (Ajax.BeginForm("Action", "Controller", FormMethod.Post, new AjaxOptions() { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "YourTargetForResult" }, new { @id = "ajaxForm" }))

要在当前视图中接收回发,必须引用jquery.unobtrusion-ajax.js。

基于您的评论的示例:

代码语言:javascript
复制
<input type="hidden" id="hdnResponseMessage" /> // add dom object where response hits

@using (Ajax.BeginForm("SetBreak", "YourControllerName", FormMethod.Post, new AjaxOptions() { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "hdnResponseMessage" }, new { @id = "form" }))
    {
            @Html.ListBoxFor(m => m.arrReasons, Model.reasonsMultiSelectList, new { @class = "form-control" })

            @Html.TextBoxFor(model => model.accessToken, new { id = "txtaccessToken" })

            @Html.TextBoxFor(model => model.campaign, new { id = "txtcampaign" })
            <br />
            <button id="btn" type="submit" class="btn btn-block bg-primary" value="Submit" >Submit</button>
            <br />
    }

康洛尔:

代码语言:javascript
复制
[HttpPost]    
public JsonResult SetBreak(breakReasonModel form)
        {
        string tok=form.accessToken;
        string cmp = form.campaign;
        string selreason = "";
        for (int i=0;i < form.arrReasons.Length;i++)
        {
             selreason = form.arrReasons[i];
        }
        SetBreak obj = new SetBreak();
        System.Collections.Generic.List<ISCampaigns> IScampaignNames = new System.Collections.Generic.List<ISCampaigns>();
        IScampaignNames = obj.setNotReadyInCampaign(tok, cmp, selreason);

        return Json("SetBreak");
        }

jQuery在文档就绪中设置侦听器:

代码语言:javascript
复制
    // add dom object listener
    $('#hdnResponseMessage').bind('DOMNodeInserted', function () {
        var txt = $('#hdnResponseMessage').text();
        if (txt == 'SetBreak')
           {
              //do your stuff here;
           }

    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60392324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档