Html.BeginForm没有将数据传递给Controller - .NET MVC

时间:2014-07-30 15:51:09

标签: asp.net-mvc razor html.beginform

我必须犯一个小错误,因为这个设置“应该”正常工作,但是当点击提交时我的Controller甚至没有在Debug中命中。

HTML

@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");
   }

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

1 个答案:

答案 0 :(得分:0)

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

你应该为html中的每个控件提供id。

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

    .
    .
    .
    .
}