当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc - MVC: Set action name using javascript fails

问题描述:

I need to set the action a form posts to using javascript. I tried to do this:

 $('form').get(0).setAttribute('action', formAction);

and

 $("#form").attr('action', formAction);

In both cases, the query is malformed, like this:localhost:xxxx//controller/action1/action2

Why does this happen? How can I correctly set the action the form posts to ?

The complete javascript function is this

function GetNotifType() {

formAction = "EditWithDifferentType";

$("#form").attr('action', formAction);

$("#form").submit();

}

网友答案:

I used @Url.Action() to get the fully qualified Url for the action, and it worked:

formAction = '@Url.Action("EditWithDifferentType", "Notifications")';
$("#form").attr('action', formAction);
$("#form").submit();
分享给朋友:
您可能感兴趣的文章:
随机阅读: