【问题标题】:asp.net mvc 4 [HttpPost] not workingasp.net mvc 4 [HttpPost] 不工作
【发布时间】:2014-08-07 20:21:25
【问题描述】:

我是 asp.net MVC 4 的新手。我在处理属性时遇到了一些问题

我在我的控制器中使用[httppost] 属性,但它不起作用

它甚至没有被调用

我的控制器

    public ActionResult Inscription()
    {
        return View();
    }


    [HttpPost]
    public ActionResult Inscription(Candidat candidat)
    {
        if (!ModelState.IsValid)
        {
            return View(candidat);

        }

        return RedirectToAction("Index");
    }

我的看法

     @model ProcRec.Models.Candidat

     @{
     ViewBag.Title = "Inscription";
     Layout = "~/Views/Shared/_Layout.cshtml";
      }


      @Html.ValidationSummary(true) 
     <div class="form_settings">
      @using (Html.BeginForm("Inscription", "CandidatController"))
      { 

      <table ="#FFFFFF">
      <tr>

      <td>@*<span >Nom :</span>*@ @Html.LabelFor(model => model.nom_candidat)</td>

      <td> @Html.TextBoxFor(model => model.nom_candidat)
            @Html.ValidationMessageFor(Model => Model.nom_candidat)     

             .

             .

       </table>

       }
  <input type="submit"  class="submit right"  value="Inscription" /> 

想你的帮助

【问题讨论】:

  • @Saranga ...我只是想完全回答这个问题..我发现了两个问题,所以我通过我的回答纠正了它们,提交按钮应该在我的回答中也有表格里面..我在安迪的回答之前回答了......谢谢......

标签: c# asp.net-mvc


【解决方案1】:

正确的 beginform 为:

@using (Html.BeginForm("Inscription", "CandidatController",FormMethod.Post))
{ 
  ........
  <input type="submit"  class="submit right"  value="Inscription" /> 
}

将提交按钮放在 BeginForm() 中,并将 BeginForm() FormMethod 作为 Post。

谢谢..

【讨论】:

    【解决方案2】:

    将您的 &lt;submit&gt; 元素放入表单中:

    @using (Html.BeginForm())
    { 
        ...
        <input type="submit"  class="submit right"  value="Inscription" /> 
    }
    

    【讨论】:

      【解决方案3】:

      不需要更改Html.BeginForm。默认情况下它是 POST。

      检查:FormExtensions.BeginForm

      唯一的问题是提交按钮,它应该在Andy Refuerzo's answer 中提到的表单内。不知道为什么它被否决。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多