MVC 使操作链接执行提交
我目前正在尝试进行 html 提交,但使用 MVC 帮助器方法 ActionLink 因为我不希望它成为一个按钮,我希望它成为一个带下划线的链接,就像我页面上的其余部分一样。这就是我目前所拥有的,
<%= Html.ActionLink("Delete Selected", "DeleteCheckBox", "Domains", "Default.aspx", new { type="submit" }) %>
这可以很好地跳回到我的操作,但是所有被选中要删除的域都不会发回。 (如果我使用这个, 它工作正常,所以我知道提交或检索支票没有问题框)
这是我到目前为止所拥有的......
>" %>指数
<h2>Domain List</h2>
<h2 style="color: #FF0000"><%= Html.Encode(ViewData[IProwlAdminUI.Utils.Global.ExceptionMessageKey]) %></h2>
<h2 style="color: #FF0000"><%= Html.Encode(ViewData["Message"]) %></h2>
<% using (Html.BeginForm("DeleteCheckBox", "Domains"))
{ %>
<% if (ViewData.ContainsKey("DeleteMessage"))
{ %>
<h2 style="color: #FF0000"><%= Html.Encode(ViewData["DeleteMessage"]) %></h2>
<input type="submit" name="DeleteAction" value="Commit" /> <input type="reset" name="DeleteAction" value="Cancel" />
<% } %>
<p>
<%= Html.ActionLink("Create New", "Create") %>
| <%= Html.ActionLink("Export List", "Export") %>
| **<a href="javascript:void(0)" class="DeleteLink">Delete Selected</a>**
<% if (ViewData.ContainsKey("Path"))
{ %>
| <%= Html.ReferenceToFile("/download/Domains.xls", "Exported File") %>
<% } %>
</p>
<div style="overflow:scroll; width:100%">
<% Html.Telerik().Grid(Model).Name("Domains")
.DataKeys(dataKeys => dataKeys.Add(c => c.DomainId)).DataKeys(dataKeys => dataKeys.Add(c => c.Name))
.Columns(columns =>
{
columns.Template(o =>
{ %>
<%= Html.ActionLink("Edit", "Edit", new { id = o.DomainId })%>
<%
}).Title("Edit");
columns.Template(o =>
{ %>
<% if (ViewData.ContainsKey("DeleteMessage"))
{ %>
<input type='checkbox' checked="checked" id='<%= o.Name %>' name='DeleteIds' value='<%= o.DomainId %>' />
<% } %>
<% else
{ %>
<input type='checkbox' id='<%= o.Name %>' name='DeleteIds' value='<%= o.DomainId %>' />
<% } %>
<%
}).Title("Delete");
columns.Bound(o => o.DomainId);
columns.Bound(o => o.Name);
columns.Bound(o => o.SiteId);
columns.Bound(o => o.ScrubAndRedirect);
columns.Bound(o => o.ReportingSiteId);
columns.Bound(o => o.TrafficCopClass);
columns.Bound(o => o.SiteName);
columns.Bound(o => o.FeedType);
columns.Bound(o => o.Active);
}).Sortable().Filterable().DataBinding(db => db.Server().Select("Index", "Domains")).Render();%>
</div>
<% if (!ViewData.ContainsKey("DeleteMessage"))
{ %>
<input type="submit" name="DeleteAction" value="Delete" />
<% } %>
<% } %>
<p>
<%= Html.ActionLink("Create New", "Create") %> | <%= Html.ActionLink("Export List", "Export") %>
<% if (ViewData.ContainsKey("Path"))
{ %>
| <%= Html.ReferenceToFile("/download/Domains.xls", "Exported File") %>
<% } %>
</p>
**<script type="text/javascript">
$(function() {
$('.DeleteLink').click(function() {
$(this).closest('form')[0].submit();
});
});
</script>**
I am currently trying to make an html submit occur, but using the MVC helper method ActionLink as I do not want it to be a button, I want it to be an underlined link like the rest on my page. This is what I have currently
<%= Html.ActionLink("Delete Selected", "DeleteCheckBox", "Domains", "Default.aspx", new { type="submit" }) %>
This jumps back to my action fine, but all the domains that are checked off to be deleted are not sent back. (if I use this, <input type="submit" name="DeleteAction" value="Delete" />
it works fine so I know it's not something wrong with submitting or retrieving the check boxes)
Here's what I have so far ...
>" %>
Index
<h2>Domain List</h2>
<h2 style="color: #FF0000"><%= Html.Encode(ViewData[IProwlAdminUI.Utils.Global.ExceptionMessageKey]) %></h2>
<h2 style="color: #FF0000"><%= Html.Encode(ViewData["Message"]) %></h2>
<% using (Html.BeginForm("DeleteCheckBox", "Domains"))
{ %>
<% if (ViewData.ContainsKey("DeleteMessage"))
{ %>
<h2 style="color: #FF0000"><%= Html.Encode(ViewData["DeleteMessage"]) %></h2>
<input type="submit" name="DeleteAction" value="Commit" /> <input type="reset" name="DeleteAction" value="Cancel" />
<% } %>
<p>
<%= Html.ActionLink("Create New", "Create") %>
| <%= Html.ActionLink("Export List", "Export") %>
| **<a href="javascript:void(0)" class="DeleteLink">Delete Selected</a>**
<% if (ViewData.ContainsKey("Path"))
{ %>
| <%= Html.ReferenceToFile("/download/Domains.xls", "Exported File") %>
<% } %>
</p>
<div style="overflow:scroll; width:100%">
<% Html.Telerik().Grid(Model).Name("Domains")
.DataKeys(dataKeys => dataKeys.Add(c => c.DomainId)).DataKeys(dataKeys => dataKeys.Add(c => c.Name))
.Columns(columns =>
{
columns.Template(o =>
{ %>
<%= Html.ActionLink("Edit", "Edit", new { id = o.DomainId })%>
<%
}).Title("Edit");
columns.Template(o =>
{ %>
<% if (ViewData.ContainsKey("DeleteMessage"))
{ %>
<input type='checkbox' checked="checked" id='<%= o.Name %>' name='DeleteIds' value='<%= o.DomainId %>' />
<% } %>
<% else
{ %>
<input type='checkbox' id='<%= o.Name %>' name='DeleteIds' value='<%= o.DomainId %>' />
<% } %>
<%
}).Title("Delete");
columns.Bound(o => o.DomainId);
columns.Bound(o => o.Name);
columns.Bound(o => o.SiteId);
columns.Bound(o => o.ScrubAndRedirect);
columns.Bound(o => o.ReportingSiteId);
columns.Bound(o => o.TrafficCopClass);
columns.Bound(o => o.SiteName);
columns.Bound(o => o.FeedType);
columns.Bound(o => o.Active);
}).Sortable().Filterable().DataBinding(db => db.Server().Select("Index", "Domains")).Render();%>
</div>
<% if (!ViewData.ContainsKey("DeleteMessage"))
{ %>
<input type="submit" name="DeleteAction" value="Delete" />
<% } %>
<% } %>
<p>
<%= Html.ActionLink("Create New", "Create") %> | <%= Html.ActionLink("Export List", "Export") %>
<% if (ViewData.ContainsKey("Path"))
{ %>
| <%= Html.ReferenceToFile("/download/Domains.xls", "Exported File") %>
<% } %>
</p>
**<script type="text/javascript">
$(function() {
$('.DeleteLink').click(function() {
$(this).closest('form')[0].submit();
});
});
</script>**
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果没有 Javascript,您无法使用超链接提交表单。
使用 jQuery,您可以编写
You cannot make a hyperlink submit a form without Javascript.
Using jQuery, you can write
添加到 SLAks,您可以使用以下方法确保您的 jQuery 代码在适当的时间触发(无论页面上的位置如何):
通过将代码包装在
$(document).ready(...) 确保页面加载完成之前代码不会运行。
Adding to SLaks, you can ensure that your jQuery code fires at the appropriate time (regardless of location on the page) by using the following:
By wrapping the code in
$(document).ready(...)
you ensure that the code will not run before the page is finished loading.您最好编写客户端 JavaScript 代码,而不是创建操作链接,该代码将在单击链接时为您提交表单。
您可以轻松地使用 jQuery 来执行此操作,使用 提交方法 选择表单的选择器:
Instead of creating an action link, you are better off writing client-side javascript code which will submit the form for you when the link is clicked.
You can easily use jQuery to do this, using the submit method on a selector which selects the form:
我看到的大多数答案都依赖于 jQuery 或执行一些花哨的 ajax 提交,这不是我想要的。所以我编写了 HtmlHelper 扩展方法,它创建带有隐藏输入和按钮的普通表单。工作正在进行中……仍然可以完成这项工作。这是类:
要使用它,请在 Razor 中编写:
结果,在 HTML 中您将得到:
Most of the answers I saw rely on jQuery or do some fancy ajax submit, which is not what I wanted. So I wrote HtmlHelper extension method that creates plain form with hidden inputs and buttons. It's work in progress... still can do the job. Here is class:
To use it, in Razor you write:
And as result, in HTML you get:
如果您使用 bootstrap,要使按钮看起来像链接,只需添加 btn-link 类,例如
if you are using bootstrap, to make a button look like a link just add the btn-link class, for example
这可以通过在 C# 中从 javascript 调用
Razor 语法
的类来完成,然后调用 Jquery 进行提交。
更新1#
很少解释我们可以在哪里使用它。
转到原始问题MVC 使操作链接执行提交
Index.cshtml 示例 chtml 查看文件
This can be done by calling class from javascript in C#
For Razor Syntax
Then call Jquery to do submit.
Update 1#
Little explanation where we can use this.
Going to Original question MVC make action link perform a submit
Index.cshtml example chtml View file
我在 Razor 中尝试了上述 Summit 的方法,但需要进行一些更改。在操作链接中包含控制器的名称会导致页面绕过 JQuery 并直接提交表单。因此,我尝试了以下操作:
从那里我可以通过 id 访问按钮并使用 onclick 方法。
我应该补充一点,我希望提交表单,但不一定希望转移到另一个页面和/或操作。
I attempted Summit's approach above in Razor but needed to make some changes. Including the Controller's name in the action link caused the page to bypass the JQuery and directly submit the form. So, I tried the following:
From there I was able to access the button by id and use the onclick method.
I should add that I wanted the form to be submitted but didn't necessarily want to be transferred to another page and/or Action.
我用其他方式做到了
我将链接放在表单标签内
并通过链接提交表格
i did it in other way
i put the link inside form tag
and submit the form by the the link