简体   繁体   English

如何通过 Javascript 发回 MVC controller 操作

[英]How do I post back to an MVC controller action via Javascript

Currently I have a aspx page that is defined as:目前我有一个 aspx 页面,定义为:

    <%Using Html.BeginForm("SaveNotifications", "Notifications", FormMethod.Post, New With {.id = "NotificationForm"})%>
    ...
                    <tr>
                <td colspan="3">
                    <input type="submit" id="SaveNotifications" name="SaveNotifications" value="Save" class="t-button" />
                </td>
            </tr>
        </table>
        </fieldset> 
    <%End Using %>

I want to change this so that my input button fires a javascript function so that I can do some complex validation of the form and THEN post to the controller.我想更改它,以便我的输入按钮触发 javascript function 以便我可以对表单进行一些复杂的验证,然后发布到 controller。

I changed my submit button to:我将提交按钮更改为:

    <input type="submit" id="SaveNotifications" name="SaveNotifications" value="Save" class="t-button" onclick="onSave(); return false;" />

The onSave() method would fire but not post back to the controller. onSave() 方法会触发但不会回发到 controller。

The controller is defined as: controller 定义为:

    <HttpPost()> _
    <MultiButton(MatchFormKey:="SaveNotifications", MatchFormValue:="Save")> _
    Function SaveNotifications(ByVal NotificationForm As FormCollection) As ActionResult
    ...
    End Function

How do I change this so that the javascript fires, runs my validation and then hits this controller action?如何更改此设置以使 javascript 触发,运行我的验证,然后执行此 controller 操作?

Thanks谢谢

Change your submit button to将您的提交按钮更改为

<input type="submit" id="SaveNotifications" name="SaveNotifications" value="Save" class="t-button" onclick="return onSave();" />

Then in the javascript onSave() function, return true or false depending on the validation.然后在 javascript onSave() function 中,根据验证返回 true 或 false。

function onSave()
{
    var isValid = true;
    // ... do your validation ...

    // ie:
    // if (field1.value.length == 0)
    //     isValid = false;


    // returning true will post the form.  False will halt it.
    return isValid;
}

暂无
暂无

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

相关问题 如何从mvc .net web app中的jquery(在自定义视图页面中)调用控制器发布操作 - How can I call controller post action from jquery (in custom view page) in mvc .net web app 如何在控制器的操作方法中重定向Ajax发布? - How do I do a redirect an Ajax Post in a Controller's Action Method? 使用Javascript在ASP.NET MVC控制器操作上通过POST下载文件(HttpPost操作) - Download a File by POST (HttpPost action) on ASP.NET MVC controller action using Javascript 如何从WebForms调用MVC中的差异控制器动作方法 - How do I call diffrent controller action method in mvc from webforms 如何在 C# MVC 中将变量从 ActionFilter 传递到控制器操作? - How do I pass a variable from an ActionFilter to a Controller Action in C# MVC? 如何将请求路由到MVC2中特定控制器中的特定操作? - How do I route a request to a specific action in a specific controller in MVC2? 使用MvcContrib TestHelper时,如何验证ASP.NET MVC2后操作? - How do I test an ASP.NET MVC2 post action with validation when using MvcContrib TestHelper? 我如何回复输入值? - how do i post back value of input? 如何将视图中的 javascript 函数中的变量传递给 ASP.NET Core 中控制器中的操作 - How do I pass variables from a javascript function in a view to an action in the controller in ASP.NET Core 如何确定MVC网站地图中的当前控制器操作? - How can I determine the current controller action in an mvc sitemap?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM