この質問をするのはばかげていると思いますが、ここでは、非常に単純な 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");
}
}