ASP.NET MVC控制器具有相同名称的操作

时间:2015-04-23 10:44:59

标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-5

我在MVC5中有一个控制器申请表格。有一个GET可以返回视图,一个POST可以从应用程序中获取表单数据并处理它: -

public ActionResult Apply(string jobReference)
{
   Apply form = new Apply();
   Session[Settings.JOBREFERENCE] = jobReference;
   return View(form);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Apply(Apply form)
{
    if (ModelState.IsValid)
    {
    }
}

当我按下提交表单时,它会调用GET控制器而不是POST控制器,尽管后者有[HttpPost]。

如果我主动重命名HTTPPost控制器并导航到它,我会看到一个服务器错误,并显示一条消息说无法找到它。提交表单会导致页面崩溃。

表单代码

    @model Salt.Models.Apply
<div class='applyForRoleExt popupBox'>
    <a href="#" class="closePopup"><img src="/img/closePopup.png" alt="close" /></a>
    <div class="innerContainer">
        @using (Html.BeginForm("Apply", "Form", FormMethod.Post, new { enctype = "multipart/form-data", @id = "ApplyForm" }))
        {
            @Html.AntiForgeryToken()
            <h6>Apply for this role</h6>
            @*<div class="haveAccount">
                    <span>Have an account? Apply quickly by logging in now</span>
                    <a href="#" class="loginApply">Login to my account</a>
                </div>*@
            <div class="leftCol">
                <label>Name<span>*</span></label>
                <div class="inputBox">
                    @Html.TextBoxFor(m => m.Name)
                </div>
                <label>Email Address<span>*</span></label>
                <div class="inputBox">
                    @Html.TextBoxFor(m => m.EmailAddress)
                </div>
                <label>Telephone Number<span>*</span></label>
                <div class="inputBox">
                    @Html.TextBoxFor(m => m.TelephoneNumber)
                </div>
                <label>Portfolio Link<span>*</span></label>
                <div class="inputBox">
                    @Html.TextBoxFor(m => m.PortfolioLink)
                </div>
            </div>
            <div class="rightCol">
                <label>CV Upload</label>
                <div class="inputBox">
                    @Html.TextBoxFor(m => m.CV, new { @type = "file" })
                </div>
                <label>Covering Note</label>
                <div class="inputArea">
                    @Html.TextAreaFor(m => m.CoveringNote)
                </div>
            </div>
            <div class="actions">
                <p class="terms">By submitting this form you consent to Salt Recruitment processing your personal data in accordance with our privacy policy and agree to future contact material.</p>
                <!--<button class="submit">Apply Now</button>-->
                <input type="submit" name="submit" value="Apply Now" />
            </div>
        }
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $.validator.unobtrusive.parse("#ApplyForm");
    });
</script>

谢谢, 麦克

1 个答案:

答案 0 :(得分:0)

它已被修复。它花了第二双眼睛,但web.config中的规则是从表单发布URL删除尾部斜杠,它默认为加载GET。不知道为什么会导致问题,但确实如此。