简体   繁体   English

ASP.NET MVC:如何在页面加载后更改Ajax.BeginForm将发布到的位置?

[英]ASP.NET MVC: How do I change where Ajax.BeginForm will post to after the page loads?

I want to make an Ajax.BeginForm that will by default, go to the add user page. 我想创建一个Ajax.BeginForm,默认情况下,转到添加用户页面。 I also want in the same form an edit and delete button. 我也希望在同一表格中有一个编辑和删除按钮。 The information in the form will be the same, I just need it to post to the different edit user and delete user URL. 表单中的信息将是相同的,我只需要它发布到不同的编辑用户并删除用户URL。

How can I modify where the form will post, depending on what submit button is pressed, while keeping all the items that Ajax.BeginForm gives us. 我如何修改表单的发布位置,具体取决于按下的提交按钮,同时保留Ajax.BeginForm为我们提供的所有项目。

Edit: 编辑:

Also wanted to note that I want the URL to be generated by my routes. 还要注意我希望URL由我的路由生成。 So in the same way the BeingForm uses the "action" and "controler" to make the path, I want to use that as well when I change the URL, so the URL is dynamic and not static. 因此,以同样的方式,BeingForm使用“action”和“controler”来创建路径,我想在更改URL时也使用它,因此URL是动态的而不是静态的。

I was able to get this to work by using jquery to change the action to the form using Url.Actions. 我能够通过使用jquery使用Url.Actions将操作更改为表单来实现此功能。

The HTML button: HTML按钮:

<button type="submit" class="btn btn-danger" id="formDeleteButton" onclick="ChangeFormToDelete()">Delete</button>

The javascript/jQuery javascript / jQuery

function ChangeFormToDelete() {
            $('#UserForm').attr('action', '@Url.Action("DeleteUser", "UserManagement")')
        }

In @Url.Action the firstparameter is the action and the second is the controller. 在@ Url.Action中,第一个参数是动作,第二个是控制器。 I made the exact same functions for edit and add and it worked perfectly. 我为编辑和添加完全相同的功能,它完美地工作。

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

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