I try to pass variable from jquery to code c# but something is wrong. I have in js this code:
<script type="text/javascript">
var mySerial = '12345';
var fooUrl = '@Url.Action("Foo", "Home")';
window.location.href = fooUrl + '?mySerial' + encodeURIComponent(mySerial);
</script>
in controller :
[HttpPost]
public ActionResult Foo(string mySerial)
{
return View();
}
After execution I keep this url: http://localhost:2214/@Url.Action("Foo",%20"Home")?mySerial12345 and I don’t understand where’s the problem, can someone help me?
You forgot the equal sign:
Notice
mySerial=instead ofmySerial.By the way it seems that you are using the WebForms view engine and not Razor, at least that’s the conclusion I draw if you see the
@Url.Actionliteral in your generated output. If this is the case please use the proper syntax according to the view engine you are using:Final remark: if this is in a separate javascript file you cannot use server side helpers such as
Url.Action. It doesn’t seem to be in a separate file because I can see the<script>inline tag but that’s what you have shown here, maybe in your actual code this is in a separate file.