【发布时间】:2019-02-19 15:39:50
【问题描述】:
我已经好几个小时在寻找解决方案了。 我正在使用 MVC 开发 C# 和 ASP.NET 应用程序。 这是一个直接邮件管理应用程序。我有一个在公司数据库中搜索重复项的页面,然后将其显示在列表中。 然后,当用户单击公司名称时,他会进入一个显示该公司重复项的页面。
为此,我在搜索页面上向我的控制器操作“Fiche”发出了 Ajax 请求,它将使用发送的参数来构建请求并返回填充了公司副本的视图模型。
该动作被调用一次,使用正确的参数,但随后被调用了两次,布尔值的参数设置为 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 或 preventDefault,但视图“Fiche”不再加载,因此在这种情况下它不是解决方案。我一定是做错了什么!
编辑:我在操作之前添加了 [HttpPost],但现在找不到视图。
【问题讨论】:
-
$(a)你能分享变量a被初始化的代码吗?或者您是否通过 ajax POST 请求为每个超链接点击配置了 api?一种解决方案是在超链接标记上添加一个 css 类,然后限制仅在单击具有该特定 css 类的超链接时才启动 ajax post 调用,例如$('a.classname') -
@Mohsin Mehmood 为每个超链接添加了事件处理程序,因此单击任何链接时都会触发单击事件,检索公司名称(单击的链接的文本)
-
如何做这样的事情:
<a href="@Url.Content("~/Doublons/Fiche")" class="doublon">@doublon.Rsoc</a>然后$('a.doublon').click(..) -
我可以,但我不确定这是问题的根源
-
这很可能是由浏览器急切获取链接引起的。我认为 Chrome 默认开启了这个功能。因此 Chrome 会自动获取链接以加快浏览速度。我建议你不要使用
a href发布数据。
标签: c# asp.net ajax model-view-controller action