Convert submit button to ActionLink

限于喜欢 提交于 2019-12-13 07:18:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!