简体   繁体   English

单击按钮调用MVC控制器和操作方法

[英]Call MVC Controller and action method on button click

I have a question about calling MVC Controller and action method on button click. 我有一个关于单击按钮时调用MVC控制器和操作方法的问题。 What I basically trying to achieve is pass some values as parameters to my action method. 我基本上想实现的目标是将一些值作为参数传递给我的操作方法。

This is my button: 这是我的按钮:

<button id="add" type="button" class="inner-button">
    <span class="fa fa-stack fa-lg">
       <i class="fa fa-circle fa-stack-2x img-responsive"></i>
       <i class="fa fa-plus fa-stack-1x fa-inverse img-responsive"></i>
    </span>
</button>

I have my code wrapped up inside a document.ready function 我将代码包装在document.ready函数中

$("#add").on("click", function () {
        var var1= $("textboxName")[0].value
        var var2= $("#textboxSurname")[0].value

        window.location.href = ('/Web/AddInformation?var1=' + var1 + '&var2=' + var2)
    });

And this is my Action method: 这是我的Action方法:

public ActionResult AddInformation(string var1, string var2)
    {
        //code
    }

The point is to allow the user to add those variables as much as they want before the saved information is wrapped up in HTTP Post request. 关键是允许用户在保存的信息打包到HTTP Post请求中之前,根据需要添加这些变量。 This is driving me crazy because I have the same button (different ID of course) and similar Jquery code that is doing the exact same thing and it work fine. 这让我发疯,因为我有相同的按钮(当然是不同的ID)和类似的Jquery代码,它们执行的操作完全相同,而且效果很好。 However this button just refuse to work. 但是,此按钮只是拒绝工作。 Is this code correct? 此代码正确吗? or did I missed something. 还是我错过了什么。

NOTE: I am aware that there is very similar question: Here But didn't find an answer to my problem there. 注意:我知道有一个非常类似的问题: 在这里,但是没有找到我的问题的答案。

i know the answer is set but its just to clear out the question 我知道答案已经确定,但只是为了清除问题

in javascript we ussually use thing like 在javascript中,我们通常使用类似

document.getElementsByName("fooName")[0].value; 

what[0]--> suggests is that you are selecting the first element with that specific name,means the page may or may not contain more with the same name [0]->表示您正在选择具有该特定名称的第一个元素,这意味着该页面可能包含或可能不包含更多具有相同名称的元素

that meaans 那个意思

textbox name=foo
textbox name==foo

would be like 就像

document.getElementsByName("foo")[0].value; 
document.getElementsByName("foo")[1].value; 

where as in jquery you dont need that right now you tried to merge jquery functionality with javascript which sometimes work and sometimes may not 像在jQuery中一样,您现在不需要它了,您尝试将jQuery功能与javascript合并,这有时会起作用,有时可能不会

when using jquery try to use standerd jquery chainnig instead of venilla javascript 使用jquery时,请尝试使用标准jquery chainnig代替venilla javascript

Use below code to get the textbox values 使用以下代码获取文本框值

var var1 = $("textboxName").val()  
var var2 = $("#textboxSurname").val()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM