MVC5绑定动作到按钮

时间:2014-01-09 10:29:32

标签: c# asp.net-mvc

将按钮绑定到动作时遇到麻烦。我创建了一个表单,当用户单击“发送”按钮时,它需要触发一些操作。

下面你可以看到我的cshtml代码:

@model Project.Models.MailMessage    
<form name="contactForm" method="post" action="" class="form-horizontal" role="form">
                <div class="col-lg-8">
                    <div id="Contact" class="jumbotron contact-form">
                        <div class="panel-heading">
                            <h1 class="panel-title">Kontakt</h1>
                        </div>
                        <div class="input-group">
                            <span class="input-group-addon">Imie</span>
                            @Html.TextBoxFor(model => model.Name, new { @class = "form-control" ,placeholder ="Twoje imię"})
                            <!--<input type="text" class="form-control" placeholder="Imie" id="inputName" required="required">-->
                        </div>

                        <div class="input-group">
                            <span class="input-group-addon">Nazwisko</span>
                            @Html.TextBoxFor(model => model.Surname, new { @class = "form-control", placeholder = "Twoje nazwisko" })
                            <!--<input type="text" class="form-control" placeholder="Nazwisko" id="inputSurname" required="required">-->
                        </div>

                        <div class="input-group">
                            <span class="input-group-addon">Email</span>
                            @Html.TextBoxFor(model => model.EmailAdress, new { @class = "form-control", placeholder = "Adres email" })
                            <!--<input type="email" class="form-control" placeholder="Email" id="inputEmail" required="required">-->

                        </div>
                        <div class="input-group">
                            <span class="input-group-addon">Treść<br /> wiadomości</span>
                            @Html.TextAreaFor(model => model.Name, new { @class = "form-control", placeholder = "Treść wiadomości", id = "inputMessage", name = "inputMessage", rows="4" })
                            <!--<textarea class="form-control" rows="4" id="inputMessage" name="inputMessage" placeholder="Treść wiadomości..." required="required"></textarea>-->
                        </div>
                        <div class="form-group">
                            <div class="col-lg-offset-2 col-lg-1">
                                <button type="submit" class="btn btn-default">
                                    Send Message
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>

这是我的控制器:

public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            return View();
        }
        // GETL /Search/
        public ActionResult Search()
        {
            return View();
        }
        public ActionResult Contact()
        {
            return View();
        }
        public ActionResult SendMail(MailMessage m)
        {

            return RedirectToAction("Contact");
        }
    }

我设法做了所有这些事情。所有代码都有效。但我坚持认为,轻松做一份工作。绑定按钮从Controller调用SendMail(MailMessage m)。

有谁能建议我怎么做?

4 个答案:

答案 0 :(得分:5)

使用Html.BeginForm助手和<input type="submit" value="..." />。查看示例here。在可能的情况下,优先考虑基础架构助手。您可能不使用Antiforgery机制来防止CSRF攻击。如the example中所述,建议在帖子表单中使用。

答案 1 :(得分:2)

您必须声明表单的action

<form ... action="@Url.Action("SendMail","Home")" ...>

答案 2 :(得分:1)

试试这个:

<强> SendMail.cshtml

     @model Project.Models.MailMessage    
     <form name="contactForm" method="post" action="" class="form-horizontal" role="form">
            <div class="col-lg-8">
                <div id="Contact" class="jumbotron contact-form">
                    <div class="panel-heading">
                        <h1 class="panel-title">Kontakt</h1>
                    </div>
                    <div class="input-group">
                        <span class="input-group-addon">Imie</span>
                        @Html.TextBoxFor(model => model.Name, new { @class = "form-control" ,placeholder ="Twoje imię"})
                        <!--<input type="text" class="form-control" placeholder="Imie" id="inputName" required="required">-->
                    </div>

                    <div class="input-group">
                        <span class="input-group-addon">Nazwisko</span>
                        @Html.TextBoxFor(model => model.Surname, new { @class = "form-control", placeholder = "Twoje nazwisko" })
                        <!--<input type="text" class="form-control" placeholder="Nazwisko" id="inputSurname" required="required">-->
                    </div>

                    <div class="input-group">
                        <span class="input-group-addon">Email</span>
                        @Html.TextBoxFor(model => model.EmailAdress, new { @class = "form-control", placeholder = "Adres email" })
                        <!--<input type="email" class="form-control" placeholder="Email" id="inputEmail" required="required">-->

                    </div>
                    <div class="input-group">
                        <span class="input-group-addon">Treść<br /> wiadomości</span>
                        @Html.TextAreaFor(model => model.Name, new { @class = "form-control", placeholder = "Treść wiadomości", id = "inputMessage", name = "inputMessage", rows="4" })
                        <!--<textarea class="form-control" rows="4" id="inputMessage" name="inputMessage" placeholder="Treść wiadomości..." required="required"></textarea>-->
                    </div>
                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-1">
                            <button type="submit" class="btn btn-default">
                                Send Message
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </form>

在这里控制器

public class HomeController : Controller
{
    //
    // GET: /Home/
    public ActionResult Index()
    {
        return View();
    }
    // GETL /Search/
    public ActionResult Search()
    {
        return View();
    }
    public ActionResult Contact()
    {
        return View();
    }
    [HttpGet]
    public ActionResult SendMail(MailMessage m)
    {
      return View();
    }
    [HttpPost]
    public ActionResult SendMail(MailMessage m)
    {
        return RedirectToAction("Contact");
    }
}

当您点击按钮时,它会在控制器中调用[HttpPost]方法。有必要提一下[HttpPost]

答案 3 :(得分:1)

只需更改方法

[HttpPost]
 public ActionResult SendMail(MailMessage m)
        {

            return RedirectToAction("Contact");
        }