超文本标记语言BeginForm不向Controller语言 - . net MVC传递数据



我一定是犯了一个小错误,因为这个设置"应该"工作,但我的控制器甚至没有在调试时点击提交。

@using (Html.BeginForm("sendMail", "LakerLegends", FormMethod.Post))
{
<table>
  <tr>
    <th class="col-xs-9">
      Name
      <br />
      <input name="name" type="text" required />
      <br />
      Email
      <br />
      <input name="email" type="text" required />
      <br />
      Category
      <br />
      <select name="category" style="font-size: 18px; padding: 0px" required>
        <option value="0">General</option>
        <option value="1">Teams</option>
        <option value="2">Events</option>
        <option value="3">Website</option>
        <option value="4">Internal</option>
      </select>
      <br />
      Details
      <br />
      <textarea name="details" cols="25" required></textarea>
      <br />
      <input type="submit" value="Save" class="form-group" />
      <br />
      <i>An officer will contact you within the next 24 hours.</i>
    </th>
    <th colspan="1">
      <img src="/Content/img/contactIcon.png" style="opacity:.7" />
    </th>
  </tr>
</table>
}
控制器

[HttpPost]
   public ActionResult sendMail(string name, string email, string category, string details)
   {
      try
      {
         return RedirectToAction("Index");
      }
      catch (Exception)
      {
         // Some code
      }
         return RedirectToAction("Index");
   }

点击提交似乎只是重新加载页面。

你可以试试这个,我希望这对你有用。

你应该给每个控件的id在html。

<input id="name" name="name" type="text" required />
控制器

[HttpPost]
public ActionResult sendMail(FormCollection loFormCollection)
{
    string name = (loFormCollection["name"] != null && loFormCollection["name"] != "") ? loFormCollection["name"] : null;
    string email = (loFormCollection["email"] != null && loFormCollection["email"] != "") ? loFormCollection["email"] : null;
    .
    .
    .
    .
}

最新更新