当我按下提交按钮时,没有回发

本文关键字:按钮 提交 | 更新日期: 2023-09-27 18:32:37

我是MVC的新手,我被困在创建提交表单上。

电子邮件模型.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; }
    }
 }
控制器

通信控制器.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>

当我按下提交按钮时,没有回发

试试这个,

 @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>

我很少使用基本助手,所以我可能是错的,但我相信你得到的默认方法

Html.BeginForm()

是一个 GET。所以,你可能想使用

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

然后,要测试您是否真的在点击控制器,请在操作中添加以下内容:

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

这将在您的验证摘要中显示为错误,这没关系,因为我们只是在这里调试。

我参加派对有点晚了...

尝试更换:

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

跟:

<button id="btnSubmit" type="submit" name="submitButton value="SendEmail" />

让我们知道这是否有效,或者您是否找到了其他解决方案! :)