You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC中Ajax POST触发Controller Action两次问题求助

解决ASP.NET MVC中Action被调用两次的问题

你的问题根源很清晰:点击<a>标签时,同时触发了AJAX POST请求和标签默认的GET跳转请求,导致Fiche Action被调用两次——第一次是带正确参数的POST请求,第二次是无参数的GET请求,自然无法获取目标数据。下面给你两种针对性的解决方案,优先推荐第一种,更贴合MVC的页面跳转逻辑。


方案1:直接使用GET请求跳转(推荐)

你的需求是跳转到新页面展示企业重复数据,完全不需要AJAX,直接生成带参数的跳转链接即可,这是MVC最常规的页面跳转方式。

步骤1:修改Razor视图中的链接,传递参数

在生成企业名称链接时,把CompareConfiguration的必要参数和rsoc一起通过Url.Action传递(注意:profileConf从前端sessionStorage获取,需要提前传到后端存在ViewModel或ViewBag里):

@foreach (var doublon in Model.ListeDoublons)
{
    <tr>
        <td class="center size-15 height-25">
            <a href="@Url.Action("Fiche", "Doublons", new { 
                rsoc = doublon.Rsoc,
                rsoc_cc = Model.CurrentCompareConfiguration.Rsoc,
                adr1 = Model.CurrentCompareConfiguration.Adr1,
                cp = Model.CurrentCompareConfiguration.CP,
                ville = Model.CurrentCompareConfiguration.Ville,
                tel = Model.CurrentCompareConfiguration.Tel,
                mail = Model.CurrentCompareConfiguration.Mail,
                user_id = Model.CurrentCompareConfiguration.User_Id,
                profileConf = ViewBag.ProfileConf
            })">@doublon.Rsoc</a>
        </td>
        <td class="center size-15 height-25">@doublon.nb_doublons</td>
    </tr>
}

步骤2:调整Action接收GET参数

去掉[HttpPost]标记(默认支持GET请求),重构参数接收逻辑:

public ActionResult Fiche(string rsoc, string rsoc_cc, string adr1, string cp, string ville, string tel, string mail, string user_id, string profileConf)
{
    // 重新构建CompareConfiguration对象
    var cc = new CompareConfiguration
    {
        Rsoc = rsoc_cc,
        Adr1 = adr1,
        CP = cp,
        Ville = ville,
        Tel = tel,
        Mail = mail,
        User_Id = user_id,
        profileConf = profileConf
    };

    bool categorie = !string.IsNullOrEmpty(cc.profileConf);
    Models.Entreprise entreprise = new Models.Entreprise();
    DataTable dt_doublons = new DataTable();
    
    if (!string.IsNullOrEmpty(rsoc))
    {
        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 = dt_doublons.Rows.Count - 1; i >= 0; i--)
        {
            if(rsoc != dt_doublons.Rows[i]["rsoc"].ToString())
            {
                dt_doublons.Rows[i].Delete();
            }
        }
        dt_doublons.AcceptChanges();
    }
    return View(getDoublons(dt_doublons));
}

步骤3:移除前端AJAX代码

现在直接通过链接跳转,不需要AJAX了,删掉原来的$(a).click事件绑定代码即可。


方案2:保留AJAX并正确处理页面渲染

如果你坚持要用AJAX,需要阻止<a>的默认跳转,然后手动处理AJAX返回的视图内容:

步骤1:修改前端点击事件

$('a').click(function (e) {
    // 阻止标签默认跳转行为
    e.preventDefault();

    // 填充你的变量值
    var raison_sociale = "";
    var adresse = "";
    var code_postal = "";
    var ville_entreprise = "";
    var telephone = "";
    var e_mail = "";
    var code_cotisant = "";
    var profileConf = sessionStorage.getItem('categ');

    var cc = { 
        Rsoc: raison_sociale, 
        Adr1: adresse, 
        CP: code_postal, 
        Ville: ville_entreprise, 
        Tel: telephone, 
        Mail: e_mail, 
        User_Id: code_cotisant, 
        profileConf: profileConf 
    };

    $.ajax({
        url: "@Url.Action("Fiche", "Doublons")",
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify({ cc: cc, rsoc: $(this).text() }),
        success: function(response) {
            // 将返回的视图HTML替换当前页面内容
            $('body').html(response);
        },
        error: function() {
            alert("请求失败,请重试!");
        }
    });
});

步骤2:确保Action和视图匹配

给Action添加[HttpPost]标记,同时确认Views/Doublons/Fiche.cshtml视图存在:

[HttpPost]
public ActionResult Fiche(CompareConfiguration cc, string rsoc)
{
    // 你的原有逻辑代码...
    return View(getDoublons(dt_doublons));
}

为什么之前阻止默认行为后视图无法加载?

因为AJAX请求返回的是视图的HTML字符串,它不会自动帮你跳转或渲染页面,你需要手动处理这个返回内容(比如替换当前页面)。而方案1用GET直接跳转,是MVC的标准页面交互方式,更简单易懂,也更符合用户的操作习惯。

内容的提问来源于stack exchange,提问作者Sebriniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:47:50