问这个问题我觉得很荒谬,但在这里,我正在尝试制作一个非常简单的 ASP.NET MVC 5 应用程序。我的第一个是善良的。我想要一个按钮,单击该按钮时会执行某些操作但不会更改用户的视图或最多返回“电子邮件已提交”消息。
我的问题是我不知道如何将按钮连接到不改变视图(即使用@Html.ActionLink()
)或提交按钮的“事件”或“动作”。我找到的每个示例也是一个提交按钮。
谢谢你的帮助。
编辑
这仍然不适合我。我将在下面发布我的代码。我的努力是基于这里所说的和链接的帖子。另外,仅供参考,我可以使其与 `@Html.ActionLink("link text", "actionname") 一起使用,但它显示为链接,我正在尝试使用“按钮”。
索引.cshtml
@{
ViewBag.Title = "Home Page";
}
<div class="hero-unit">
<h3>Marketing & Communications Project Request Form</h3>
<p>User: <strong>@Context.User.Identity.Name</strong></p>
</div>
<div class="row">
<div class="span4">
@Html.ActionLink("Send Email", "SendEmail") @*this line works*@
<input id="SendEmail" class="btn" type="button" value="SendEmail" /> @*this line does not
</div>
</div>
<script type="text\javascript">
$(function(){
var url = '@Url.Action("SendEmail", "HomeController")';
$("input#SendEmail").on('click', function() {
$.ajax({
url: url
})
});
});
</script>
家庭控制器
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
public ActionResult SendEmail()
{
//Code to create and send email here
return View("Index");
}
}