将提交按钮转换为ActionLink [英] Convert submit button to ActionLink
本文介绍了将提交按钮转换为ActionLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用 ActionLink
转换提交按钮?
Is there a way to convert the submit button with an ActionLink
?
我得到了这个 ActionLink
,它将用户重定向到索引页面:
I got this ActionLink
which redirects the user to the index page:
@Html.ActionLink("Cancel", "Index", null, new { @class = "k-button" })
还有一个保存按钮,用于保存创建或更改的内容:
And a save button which submits to save whatever is created or altered:
<input type="submit" value="Save" />
最好将提交按钮的代码与 ActionLink
相同.
It would be nice to have the submit button the same code as the ActionLink
.
推荐答案
您可以直接使用< a>
标记,尝试执行以下操作:
You could use an <a>
tag directly, try something like this:
<a onclick="$('#YourForm').submit();" class="k-button">Save</a>
在您的表单的操作帖子中:
And in your Action Post of your form:
[HttpPost]
public ActionResult Post(..)
{
/// process something...
return RedirectToAction("Index");
}
HTML帮助器
就像在评论中一样,您可以创建自己的html帮助器,试试这个:
HTML Helper
As in the comments, you could create your own html helper, try this:
using System;
using System.Web.Mvc;
namespace MvcApplication.Helpers
{
public static class HtmlExtensions
{
public static string SubmitLink(this HtmlHelper helper, string text, string formId)
{
return string.Format("<a class='k-button' onclick='$(\"#{1}\").submit();'>{1}</a>", text, formdId);
}
}
}
并使用它:
@Html.SubmitLink("Save", "formId")
这篇关于将提交按钮转换为ActionLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文