Asp.net mvc - Html 提交按钮未随邮件发送?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5663385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:58:56  来源:igfitidea点击:

Asp.net mvc - Html Submit button not sent with post?

htmlasp.net-mvc

提问by

As in the title. I use thismethod for allowing more than one submit button per form and more than one action to handle them.

如标题所示。我使用这种方法允许每个表单有多个提交按钮和多个操作来处​​理它们。

However - for some odd reason - all of a sudden my submit button doesn't get submitted with the form!? any reason why that would happen?

但是 - 出于某种奇怪的原因 - 突然间我的提交按钮没有与表单一起提交!?为什么会发生这种情况?

tested using IE, FF, Chrome - all latest

使用 IE、FF、Chrome 进行测试 - 都是最新的

View code:

查看代码:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Setup.Mvc.ViewModels.SignUpViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    SignUp
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm("SignUp", "SetUp", FormMethod.Post))
       {

    %>
    <%: Html.ValidationSummary()%>
    <button  type="submit" value="Register" name="ActionRegister" >Register</button>
    <button  type="submit" value="Cancel" name="ActionCancel" class="cancel" >Cancel</button>
    <fieldset>
        <legend>Account Information </legend>
        <dl>
            <dt>
                <%:Html.LabelFor(x=>x.UserName)%>
            </dt>
            <dd>
                <%: Html.TextBoxFor(x=>x.UserName) %>
                <%: Html.ValidationMessageFor(model => model.UserName) %>
            </dd>
            <% if (string.IsNullOrEmpty(Model.Provider))
               {    
            %>
            <dt>
                <%:Html.LabelFor(x=>x.Password)%>
            </dt>
            <dd>
                <%: Html.PasswordFor(x => x.Password)%>
                <%: Html.ValidationMessageFor(model => model.Password) %>
            </dd>
            <%
               } %>
            <dt>
                <%:Html.LabelFor(x=>x.Email)%>
            </dt>
            <dd>
                <%: Html.TextBoxFor(x=>x.Email) %>
                <%: Html.ValidationMessageFor(model => model.Email) %>
            </dd>
            <dt>
                <%:Html.LabelFor(x=>x.Themes) %>
            </dt>
            <dd>
                <%: Html.DropDownList("ThemeId", Model.Themes.Select(x=> new SelectListItem{ Text = x.DisplayName, Value = x.DisplayName}), "Select a Theme") %>
                <%: Html.ValidationMessageFor(model => model.Themes) %>
            </dd>
        </dl>
    </fieldset>
    <fieldset>
        <legend>
            <h2>
                Additional Information</h2>
        </legend>
        <dl>
            <dt>
                <%:Html.LabelFor(x=>x.PublicSite)%>
            </dt>
            <dd>
                <%:Html.CheckBoxFor(x => x.PublicSite)%>
                <%: Html.ValidationMessageFor(model => model.PublicSite) %>
            </dd>
            <dt>
                <%:Html.LabelFor(x=>x.SiteUrl)%>
            </dt>
            <dd>
                http://cheesyurl.com/<%:Html.TextBoxFor(x => x.SiteUrl)%>
                <%: Html.ValidationMessageFor(model => model.SiteUrl) %>
            </dd>
        </dl>
    </fieldset>
    <%
       }%>
</asp:Content>

Action:

行动:

    [HttpPost]
    [ActionName("SignUp")]
    [UnitOfWork]
    [AcceptParameter(Action = "ActionRegister")]
    public ActionResult SignUp_Register(SignUpViewModel vm)
    {
        return View();
    }

I tried changing it from submit to button and it worked for all of 2 posts and then stopped?!

我尝试将其从提交更改为按钮,它适用于所有 2 个帖子,然后停止了?!

i'm completely lost for reasons... i bet i'm doing soemthing completely stupid...

由于某些原因,我完全迷失了……我敢打赌我在做一些完全愚蠢的事情……

Post output from firebug:

发布来自萤火虫的输出:

Parametersapplication/x-www-form-urlencoded
Email   [email protected]
Password    sdfsdfsdfsdf
PublicSite  false
SiteUrl sdfsdfsdf
ThemeId 
UserName    Usernames-12131
Source
Content-Type: application/x-www-form-urlencoded Content-Length: 112 UserName=Usernames-Sux0r&Password=sdfsdfsdfsdf&Email=wayne%40isit.gd&ThemeId=&PublicSite=false&SiteUrl=sdfsdfsdf

采纳答案by Rob Stevenson-Leggett

Have you set the name on the input?

您是否在输入上设置了名称?

Form fields don't get sent unless you specify the "name" attribute.

除非您指定“名称”属性,否则不会发送表单字段。

<input type="submit" name="Delete" value="Delete" />
<input type="submit" name="Edit" value="Delete" />

EDIT: I see you're using buttons rather than submit. Which might be the problem.

编辑:我看到您正在使用按钮而不是提交。这可能是问题所在。

You need to have use inputs like this:

您需要使用这样的输入:

<input  type="submit" value="Register" name="ActionRegister"/>
<input  type="submit" value="Cancel" name="ActionCancel" class="cancel" />

回答by A P

The way I have done this before is I post my form to same action. In my action I check the value of input button.

我之前这样做的方式是将我的表单发布到相同的操作中。在我的操作中,我检查输入按钮的值。

So In your case you should do following on your View. Keep the name of both input submit same but different value.

因此,在您的情况下,您应该在您的视图上执行以下操作。保持两个输入的名称提交相同但不同的值。

<% using (Html.BeginForm("SignUp", "SetUp", FormMethod.Post))
{%>
<%: Html.ValidationSummary()%>
     <input type="submit" value="Register" name="actionName" />
     <input  type="submit" value="Cancel" name="actionName" class="cancel">Cancel</input>
}

And on your controller

在你的控制器上

public ActionResult SignUp_Register(SignUpViewModel vm,string actionName)
{
    if(actionName =="Register")
          //Do something;
    if(actionName =="Cancel")
          //Do something;

    return View();
}

In standard cases these should work....

在标准情况下,这些应该工作......

Let me highlight something interesting now...!!

现在让我强调一些有趣的事情......!

1)On Submit event of the form if you are disabling all input type of submit in your JavaScript to disable double submit, then you will always get null values for them in the controller as they are disabled.

1)在表单的提交事件上,如果您在 JavaScript 中禁用所有输入类型的提交以禁用双重提交,那么当它们被禁用时,您将始终在控制器中获得它们的空值。

2)I realized that you have cancel class on your submit which has some unintended behavior if you are using jQuery validation. Ref: http://docs.jquery.com/Plugins/Validation/Reference

2)我意识到您在提交时取消了类,如果您使用 jQuery 验证,这会出现一些意外行为。参考:http: //docs.jquery.com/Plugins/Validation/Reference

Hope this helps..!!

希望这可以帮助..!!

回答by A P

This:

这个:

<button  type="submit" value="Register" name="ActionRegister" >Register</button>
<button  type="submit" value="Cancel" name="ActionCancel" class="cancel" >Cancel</button>

Seems to work?! wtf?

好像有用?!卧槽?

回答by Fernando Becerra

You need to set equal the id of the button (View) and the name of the variable to receive (Controller) The value of the button will be sent to the variable

您需要设置等于按钮的 id (View) 和要接收的变量的名称 (Controller) 按钮的值将被发送到变量

    <button type="submit" id="btnRegister" name="btn" class="btn btn-primary" value="Register">
        Register
    </button>
    <button type="submit" id="btnCancel" name="btn" class="btn btn-primary" value="Cancel">
        Cancel
    </button>

And your controller:

还有你的控制器:

    [HttpPost]
    public ActionResult SignUp_Register(string btn, SignUpViewModel vm m)

variable btn will be either "Register" or "Save" depending on the button clicked

变量 btn 将是“注册”或“保存”,具体取决于单击的按钮