Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC5 binding action to button

Tags:

c#

asp.net-mvc

I have troubles with binding my button to an action. I created a form and when user click "send" button it needs to trigger some action.

Below you can see my cshtml code:

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

Here is my Controller:

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");
        }
    }

I managed to do all this things. All code works. But stucked with I think easies part tof a job. Binding button to call SendMail(MailMessage m) from Controller.

Can anyone suggest me how to do this?

like image 491
szpic Avatar asked Jul 30 '26 11:07

szpic


1 Answers

Use Html.BeginForm helper and <input type="submit" value="..." /> within. See an example here. Prefer infrastructure helpers over own HTML code where possible. You probably do not use Antiforgery mechanics to prevent CSRF attacks. It's recommended in post forms, as described in the example.

like image 83
Marcin Wachulski Avatar answered Aug 02 '26 00:08

Marcin Wachulski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!