Youness Benbiga Youness Benbiga - 2 months ago 19
C# Question

using methods with href

well i m having 2 hyperlinks :

<a href="@Url.Action("Fr", "Home")">Fr</a>
|
<a href="@Url.Action("Ang", "Home")">Ang</a>


in my controller i used this to change the current langage

[HttpPost]
public ActionResult Fr()
{
ContentModelView CMV = new ContentModelView();
//Langue:
int CurrentLanguageid = db.Langues.FirstOrDefault(x => x.active_langue == "true").id_langue;
Langues LangueA = db.Langues.Find(CurrentLanguageid);
LangueA.active_langue = "false";

db.SaveChanges();
int NextLanguage = db.Langues.FirstOrDefault(x => x.txt_langue == "fr").id_langue;
Langues LangueB = db.Langues.Find(CurrentLanguageid);
LangueB.active_langue = "true";

db.SaveChanges();



return (RedirectToAction("../Home/Index"));
}

[HttpPost]
public ActionResult Ang()
{

ContentModelView CMV = new ContentModelView();
//Langue:
int CurrentLanguageid = db.Langues.FirstOrDefault(x => x.active_langue == "true").id_langue;
Langues LangueA = db.Langues.Find(CurrentLanguageid);
LangueA.active_langue = "false";

db.SaveChanges();
int NextLanguage = db.Langues.FirstOrDefault(x => x.txt_langue == "en").id_langue;
Langues LangueB = db.Langues.Find(CurrentLanguageid);
LangueB.active_langue = "true";

db.SaveChanges();

return (RedirectToAction("../Home/Index"));
}


but i don t know if i m obliged to use a parametre in my methode because it s an httpost and it send me to /Home/Fr not the index even after forcing the method no change in database i m struggling with this problem

Answer

Clicking on the link element(anchor tag) will issue a GET request.Your action methods are marked with HttpPost and it will not be hit from your GET action request generated by clicking in the link. You have 2 options

  1. Change remove the [HttpPost] decorator from the action method
  2. Hijack the click event on the link and make an http post ajax call.

Also i notice that you have duplicate code in the two methods except the language code you use in your where clause. Why not use a single method with a parameter where you can pass the language code ?

public ActionResult UpdateLanguage(string id)
{
    //This is your existing code. I did not verify the correctness of this!
    var CMV = new ContentModelView();
    //Langue:
    int CurrentLanguageid = db.Langues.FirstOrDefault(x => x.active_langue == "true")
                                                                               .id_langue;
    Langues LangueA = db.Langues.Find(CurrentLanguageid);
    LangueA.active_langue = "false";

    db.SaveChanges();
    int NextLanguage = db.Langues.FirstOrDefault(x => x.txt_langue == id).id_langue;
    Langues LangueB = db.Langues.Find(CurrentLanguageid);
    LangueB.active_langue = "true";

    db.SaveChanges();

    if(Request.IsAjaxRequest())
        return Json(new { status="success"},JsonRequestBehavior.AllowGet);

    return RedirectToAction("Index","Home");
}

Now, to make the ajax call, you may simply add a css class to the links

<a class="languageLink"  href="@Url.Action("UpdateLanguage", "Home",new {id="fr"})">Fr</a>
<a class="languageLink"  href="@Url.Action("UpdateLanguage", "Home",new {id="en"})">En</a>

Now the js code to hijack the link click event and make an ajax call

$(function(){

   $("a.languageLink").click(function(){

     $.post($(this).attr("href"),function(data){
       window.location.href="@Url.Action("Index","Home")";
     });

   });

});