从Ajax帖子调用两次控制器操作

问题描述 投票:0回答:1

我需要几个小时才能找到解决方案。我正在使用MVC开发一个C#和ASP.NET应用程序。这是一个直接邮件管理应用程序。我有一个页面,在公司数据库中搜索重复项,然后将其显示在列表中。然后,当用户点击公司名称时,他会登陆显示该公司重复项的页面。

为此,在搜索页面上,我向我的控制器操作“Fiche”发出了一个Ajax请求,该操作将使用发送的参数来构建请求并返回填充了公司副本的viewmodel。

使用正确的参数调用该动作一次,但随后,它被调用两次,参数设置为布尔值为false,字符串为null。因此,我无法检索公司的重复项。这是我的点击事件:

$(a).click(function () {
        //some code that sets the variables used in cc
        var cc = {
            rsoc: raison_sociale,
            adr1: adresse,
            cp: code_postal,
            ville: ville_entreprise,
            tel: telephone,
            mail: e_mail,
            user_id: code_cotisant,
            profileConf: sessionStorage.getItem('categ')
        }
        $.ajax({
            url: "@Url.Action("Fiche", "Doublons")",
            type: "POST",
            contentType: "application/json",
            data: JSON.stringify({ cc: cc, rsoc: $(this).text() }),
            success: function(response) {
                response ? alert("It worked!") : alert("It didn't work.");
            }
        });
    })

这是我的控制器动作:

public ActionResult Fiche(CompareConfiguration cc, string rsoc)
    {
        bool categorie = cc.profileConf != null ? true : false;
        Models.Entreprise entreprise = new Models.Entreprise();
        DataTable dt_doublons = new DataTable();
        if (rsoc != null)
        {
            dt_doublons = entreprise.search_doublons(cc.Rsoc, cc.Adr1, cc.CP, cc.Ville, cc.Tel, cc.Mail, cc.User_Id, categorie, cc.profileConf.Split(','));
            for (int i = 0; i < dt_doublons.Rows.Count; i++)
            {
                if(rsoc != dt_doublons.Rows[i]["rsoc"].ToString())
                {
                    dt_doublons.Rows[i].Delete();
                }
            }
            dt_doublons.AcceptChanges();
        }

        return View(getDoublons(dt_doublons));
    }

    private DoublonsViewModel getDoublons(DataTable dt_doublons)
    {
        DoublonsViewModel dblVM = new DoublonsViewModel()
        {
            ListeDoublons = new List<EntrepriseAndContacts>(),
            dt_doublons = dt_doublons
        };

        for (int i = 0; i < dt_doublons.Rows.Count; i++)
        {
            EntrepriseAndContacts eac = new EntrepriseAndContacts();

            eac.Id = Convert.ToInt32(dt_doublons.Rows[i]["id_entreprise"]);
            eac.Rsoc = dt_doublons.Rows[i]["rsoc"].ToString();
            eac.nb_doublons = Convert.ToInt32(dt_doublons.Rows[i]["nb_doublons"]);
            eac.Etat_entreprise = Convert.ToInt32(dt_doublons.Rows[i]["importee"]);
            eac.Etat_contact = Convert.ToInt32(dt_doublons.Rows[i]["importe"]);
            eac.User_id = dt_doublons.Rows[i]["user_id"].ToString();
            eac.CVI = dt_doublons.Rows[i]["cvi"].ToString();
            eac.Nom = dt_doublons.Rows[i]["nom"].ToString();
            eac.Prenom = dt_doublons.Rows[i]["prenom"].ToString();
            eac.Mail = dt_doublons.Rows[i]["mail"].ToString();

            dblVM.ListeDoublons.Add(eac);
        }

        return dblVM;
    }

和链接:

foreach (var doublon in Model.ListeDoublons)
  {
    <tr>
      <td class="center size-15 height-25">
        <a href="@Url.Content("~/Doublons/Fiche")">@doublon.Rsoc</a>
      </td>
      <td class="center size-15 height-25">@doublon.nb_doublons</td>
    </tr>
  }

我试图返回false或者阻止对click事件的默认,但是视图“Fiche”不再被加载,因此在这种情况下它不是解决方案。我一定做错了什么 !

编辑:我在操作之前添加了[HttpPost],但现在找不到视图。

c# asp.net ajax model-view-controller action
1个回答
0
投票

希望这次我可以发布答案,因为我找到了解决问题的方法。我在动作Fiche之前移除了[HttpPost]并且在第一次传入方法时,我将参数cc和rsoc存储在两个会话变量中。然后,我将它重新分配给cc和rsoc,所以当它第二次传递方法时cc和rsoc为空,它会通过会话检索它们。这不是一个很好的解决方案,但我没有时间离开,它的工作原理。

public ActionResult Fiche(CompareConfiguration cc, string rsoc)
    {
        if(cc.Adr1 != false || cc.Rsoc != false || cc.CP != false || cc.Ville != false || cc.Tel != false || cc.Mail != false || cc.User_Id != false)
        {
            Session["cc"] = cc;
            Session["rsoc_entreprise"] = rsoc;
        }

        cc = (CompareConfiguration)Session["cc"];
        rsoc = Session["rsoc_entreprise"].ToString();
        bool categorie = cc.profileConf != null ? true : false;
        Models.Entreprise entreprise = new Models.Entreprise();
        DataTable dt_doublons = new DataTable();
        if (rsoc != null)
        {
            dt_doublons = entreprise.search_doublons(cc.Rsoc, cc.Adr1, cc.CP, cc.Ville, cc.Tel, cc.Mail, cc.User_Id, categorie, cc.profileConf.Split(','));
            for (int i = 0; i < dt_doublons.Rows.Count; i++)
            {
                if(rsoc != dt_doublons.Rows[i]["rsoc"].ToString())
                {
                    dt_doublons.Rows[i].Delete();
                }
            }
            dt_doublons.AcceptChanges();
        }

        return View(getDoublons(dt_doublons));
    }
© www.soinside.com 2019 - 2024. All rights reserved.