C# MVC表单未过帐,正在调用GET

C# MVC表单未过帐,正在调用GET,c#,.net,asp.net-mvc,razor,model-view-controller,C#,.net,Asp.net Mvc,Razor,Model View Controller,我无法让我的表单调用控制器上的POST操作 我的页面代码 @model My.Models.ManageUserViewModel @using (Html.BeginForm("Manage", "Account", FormMethod.Post, new { @id = "manageAccountFormHolder", @class = "form-horizontal col-sm-8 col-sm-offset-2 v-offset-4" })) { @H

我无法让我的表单调用控制器上的POST操作

我的页面代码

@model My.Models.ManageUserViewModel

@using (Html.BeginForm("Manage", "Account", FormMethod.Post, new { @id = "manageAccountFormHolder", @class = "form-horizontal col-sm-8 col-sm-offset-2 v-offset-4" }))
    {
        @Html.AntiForgeryToken()
        <h4 class="text-center">Change Password</h4>

        <div class="form-group">
            @Html.LabelFor(m => m.OldPassword, new { @class = "col-sm-3 control-label" })
            <div class="col-sm-9">
                @Html.PasswordFor(m => m.OldPassword, new { @class = "form-control" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(m => m.NewPassword, new { @class = "col-sm-3 control-label" })
            <div class="col-sm-9">
                @Html.PasswordFor(m => m.NewPassword, new { @class = "form-control" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-sm-3 control-label" })
            <div class="col-sm-9">
                @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
            </div>
        </div>

        <h4 class="text-center v-offset-4">Edit Personal Details</h4>

        <div class="form-group">
            @Html.LabelFor(m => m.FirstName, new { @class = "col-sm-3 control-label" })
            <div class="col-sm-4">
                @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control", @placeholder = "First name" })
            </div>
            <div class="col-sm-5">
                @Html.TextBoxFor(m => m.LastName, new { @class = "form-control", @placeholder = "Last name" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(m => m.Mobile, new { @class = "col-sm-3 control-label" })
            <div class="col-sm-9">
                <input class="form-control" id="manageMobile" name="manageMobile" type="tel">
                @Html.TextBoxFor(m => m.Mobile, new { @class = "form-control", @placeholder = "First name" })
                <p class="help-block">For authorisation code. We will never share or display your mobile number.</p>
            </div>
        </div>

        <h4 class="text-center v-offset-4">Edit Address</h4>

        <div class="form-group">
            @Html.LabelFor(m => m.Address1, new { @class = "col-sm-3 control-label" })
            <div class="col-sm-2">
                @Html.TextBoxFor(m => m.Address1, new { @class = "form-control"})
            </div>
            <div class="col-sm-7">
                @Html.TextBoxFor(m => m.Address2, new { @class = "form-control" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(m => m.TownSuburb, new { @class = "col-sm-3 control-label" })
            <div class="col-sm-9">
                @Html.TextBoxFor(m => m.TownSuburb, new { @class = "form-control" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(m => m.StateRegion, new { @class = "col-sm-3 control-label" })
            <div class="col-sm-9">
                @Html.TextBoxFor(m => m.StateRegion, new { @class = "form-control" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(m => m.PostCode, new { @class = "col-sm-3 control-label" })
            <div class="col-sm-9">
                @Html.TextBoxFor(m => m.PostCode, new { @class = "form-control" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(m => m.Country, new { @class = "col-sm-3 control-label" })
            <div class="col-sm-9">
                @Html.TextBoxFor(m => m.Country, new { @class = "form-control" })
                @Html.HiddenFor(m => m.CountryIso, new { @class = "form-control" })
            </div>
        </div>

        <div class="text-center">
            <button class="btn btn-default" type="submit">Save Changes</button>
        </div>
    }
我的控制器动作

[HttpGet]
public ActionResult Manage(ManageMessageId? message)
{
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Manage(ManageUserViewModel model)
{
    //Save changes code here
    //Redirect
}
它也不会验证。如果我将其中一个必填字段留空,则不会显示验证错误

在Fiddler中,我看到表单正在发布到服务器,但仍然调用GET方法?请参见下面的Fiddler raw,它仍然调用GET方法(我只将模型更新为一个属性以使其更简单)


将按钮更改为“输入”

<input class="btn btn-default" type="submit" value="Save Changes" />

这是固定的!如果有人能告诉我为什么,因为我有NFI

换这条线

@using (Html.BeginForm("Manage", "Account", FormMethod.Post, new { @id = "manageAccountFormHolder", @class = "form-horizontal col-sm-8 col-sm-offset-2 v-offset-4" }))

我又在发帖了。是的,编码天才,4小时后发布表单!有人想雇用我做一份工作吗

编辑:


因此,在进一步挖掘之后,问题是我们已经实施了小写路径。由于路由是从/Account/Manage重写到/Account/Manage的,因此找不到post操作(尽管找到get操作很好)。Html.BeginForm()以小写形式呈现操作,而Html.BeginForm(“Manage”、“Account”、FormMethod.Post、new{})不是,因为我为操作和控制器指定了驼峰大小写。

您使用的两种不同的
BeginForm
方法是:

,这意味着

@using (Html.BeginForm("Manage", "Account", FormMethod.Post, new {}))
{
    // Produces the following form element
    // <form action="/Account/Manage" action="post">
}

所以,它应该是有效的,真的。我创建它是为了找出差异。

ManageMessageId的结构是什么?它是一个枚举。。。。。。。。。public enum ManageMessageId{ChangePasswordSuccess,SetPasswordSuccess,removeloginsucces,Error}@garethb-您的代码运行完全正常,没有任何更改。您应该在浏览器控制台中查找错误。@SBirthare-请参阅更新,Fiddler用数据显示表单过帐。但是我的GET操作是hit not POST(两个操作中的断点)。可能是HTTPS不允许post吗?您是否尝试更改输入类型=“submit”-确定是否可以删除Get方法以检查发布表单时出现的错误?我将Get放回并将post上的操作名称更改为ManageSave(在控制器和表单操作中)还有找不到Get资源?只删除Get,不重命名任何内容。是的,先尝试一下。这就是我得到的资源找不到错误。所以它识别出该操作是post only。之后尝试了名称更改。我复制了你的代码,它工作得非常好。奇怪。。。我能理解你的沮丧,我们有时不得不在编程中看到这样的日子。也许其他人会通过一些线索来了解真正的问题是什么?在你最初的问题中,有什么迹象表明这篇文章不起作用。即,您是否收到空白页?您在
ActionResult Manage(ManageUserViewModel模型)
中是否有未命中的断点?@ShaunLuttin-我在post和get操作中都有断点。只有被击中。Fiddler告诉我表单是从浏览器发布的。当您明确给出操作名称和控制器名称时,表单将发布到特定的控制器和操作。如果省略操作名和控制器名,则将表单发布到URL。这是我在这里看到的唯一区别。我想您的控制器名称可能造成了问题。请更正小写问题。关于如何仅将GET URL转换为小写的解决方案,请参见此处的答案,这解决了问题:我本以为表单操作在您的小提琴中会是另一种方式?第二种形式的动作有索引动作,我原以为第一种应该是索引动作。我的表单现在使用BeginForm(HtmlHelper)发布,但是只要我使用BeginForm(HtmlHelper,String,String,FormMethod,IDictionary),而不更改任何其他内容,get就会被调用。当我有时间的时候,我可能会深入研究这个问题!
@using (Html.BeginForm("Manage", "Account", FormMethod.Post, new { @id = "manageAccountFormHolder", @class = "form-horizontal col-sm-8 col-sm-offset-2 v-offset-4" }))
@using (Html.BeginForm())
@using (Html.BeginForm("Manage", "Account", FormMethod.Post, new {}))
{
    // Produces the following form element
    // <form action="/Account/Manage" action="post">
}
@using (Html.BeginForm())
{
    // Produces the following form element
    // <form action="/Account/Manage" action="post">
}