问题
Is there a way to convert the submit button with an 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" />
It would be nice to have the submit button the same code as the ActionLink
.
回答1:
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 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);
}
}
}
And use it:
@Html.SubmitLink("Save", "formId")
来源:https://stackoverflow.com/questions/14704965/convert-submit-button-to-actionlink