0

私は MVC を初めて使用し、送信フォームの作成に行き詰まっています。

モデル Email.cs:

 using System.Web;

 namespace MySite.Models
 {
    public class Email
    {
       public string From { get; set; }
       public string Subject { get; set; }
       public string body { get; set; }
    }
 }

コントローラー通信Controller.cs:

namespace MySite.Controllers
{
    public class CommunicationController : Controller
     { 
        public ActionResult SendEmail() {

        Email email = new Email();
        return View(email);
     }

      [HttpPost]
      public ActionResult SendEmail(Email email)
       {
          if (ModelState.IsValid)
           { 

            }

            return View(email);
        }
    }
}

SendEmail.cshtml を表示します。

@model MySite.Models.Email

@{
   ViewBag.Title = "SendEmail";
 }

<h2>@Html.Label("Send email")</h2>

@using(Html.BeginForm())
 {
   @Html.AntiForgeryToken()
   @Html.ValidationSummary(true)    
   <div class="editor-label">
       @Html.Label("From")
   </div>
   <div class="editor-field">
      @Html.EditorFor(model => model.From)

  </div>  <div class="editor-label">
    @Html.Label("Subject")
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Subject)

  </div>   <div class="editor-label">
      @Html.Label("Body")
   </div>
  <div class="editor-field">
      @Html.EditorFor(model => model.body)

   </div>

   <input id="btnSubmit" type="submit" value="SendEmail" />
}

送信を押すと、イベントは発生しません。コントローラーで「ビューに移動」を押すと、SendEmail ビューに移動します。何が起こっているのかわかりません。デバッグしようとしましたが、[HttpPost] コントローラーが起動しません。

ブラウザから取得したものは次のとおりです。アクションが表示されません

<form method="post" action="/" novalidate="novalidate">
    <input type="hidden" value="ZktM_I7fzdlcNme4YVEcNNpnFFmQu1cpAuTXarO_V4w-7bPmpHkaLRfNY3cXGMYy7wkRgSJWW‌​SkS8lp5vdRimFrNCgqk0Jfdr4v7Zc3V2pg1" name="__RequestVerificationToken">
    <div class="editor-label">
        <div class="editor-field">
            <input id="From" class="text-box single-line" type="text" value="" name="From">
        </div>
        <div class="editor-label">
            <div class="editor-field">
                <div class="editor-label">
                    <div class="editor-field">
                        <input id="btnSubmit" type="submit" value="SendEmail">
</form>
4

3 に答える 3

0

私は基本的なヘルパーをめったに使用しないので、間違っている可能性がありますが、デフォルトの方法はあなたが取得したものだと思います

Html.BeginForm()

GETです。したがって、おそらく使用したいでしょう

Html.BeginForm("SendEmail", "Communication", FormMethod.Post, new { /* html attributes */ })

次に、実際にコントローラーを叩いているかどうかをテストするために、アクション内にこれを追加します。

ModelState.AddModelError("", "Action was called!");

これは ValidationSummary にエラーとして表示されますが、ここでデバッグしているだけなので問題ありません。

于 2013-09-25T04:35:01.163 に答える
0

これを試して、

 @using (Html.BeginForm("ActionName", "ControllerName",
                    FormMethod.Post))
        {
           //form UI

           <input id="btnSubmit" type="submit" value="SendEmail" />
        }

編集

これを試すこともできます:

<a onclick="$('#formId').submit();">Submit</a>
or
<button type="submit">Submit</button>
于 2013-09-25T04:08:06.013 に答える