简体   繁体   English

具有相同名称的ASP.NET MVC Controller操作

[英]ASP.NET MVC Controller actions with the same name

I've got a controller in MVC5 for an apply form. 我在MVC5中有一个申请表的控制器。 There is a GET to return the view and a POST to take the form data from the application and process it:- 有一个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)
    {
    }
}

When I press the to submit the form, it calls the GET controller instead of the the POST controller, despite it having [HttpPost] on the latter. 当我按提交表单时,尽管后者具有[HttpPost],但它会调用GET控制器而不是POST控制器。

If I actively renamed the HTTPPost controller and navigate to it, I see a Server Error, with a message saying it cannot be found. 如果我主动重命名了HTTPPost控制器并导航到它,则会看到服务器错误,并显示一条消息,指出找不到该错误。 Submitting the form causes the page to crash. 提交表单会使页面崩溃。

Form Code 表格代码

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

Thanks, Mike. 谢谢,迈克。

It's been fixed. 已修复。 It took a second pair of eyes but a rule in the web.config was removing the trailing slash from the form post URL and it was defaulting to loading the GET. 花了第二眼,但web.config中的一条规则是从表单发布URL中删除尾部斜杠,并且默认情况下是加载GET。 No idea why that would cause the problem but it did. 不知道为什么会导致问题,但确实如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM