10/15/14

How to submit form value in mvc


How to submit form value

The value can be submitted through submit button and ajax button

I have create one form which has below which has one input box and two button one submit and other normal button

@model WebApplication2.Models.Person

 

@{

    ViewBag.Title = "Index";

}

 

<script src="~/Scripts/jquery-1.10.2.js"></script>

<script>

    $(document).ready(function () {

        $("#Button2").click(function () {

            $.ajax({

                url: "/Home/Method",

                type: 'POST',

                data: $('form').serialize(),

                success: function (response) {

                    document.getElementById("Firstname").value = response

                },

                error: function (xhr) {

                    alert("Something went wrong, please try again");

                }

            });

 

        });

 

    });

 

</script>

@using (@Html.BeginForm("Index", "Home", FormMethod.Post, new { autocomplete = "off" }))

{

 

    <div class="jumbotron">

        @Html.TextBoxFor(model => model.Firstname)

 

        <br />

        <br />

        <br />

        <input id="Button1" type="submit" value="SubmitBtn" />

        <br />

        <input id="Button2" type="button" value="Enter" />

 

 

    </div>

 

}

 

Model

   public class Person

    {

        public string Firstname { get; set; }

    }

Controller code

        [HttpPost]

        public string Index(Person person)

        {

            string form = "Submit response ";

            return form;

        }

        [HttpPost]

        public string Method(Person person)

        {

            string form = person.Firstname;

 

            return form;

        }

To submit the form

@using (@Html.BeginForm("Index", "Home", FormMethod.Post, new { autocomplete = "off" }))

 

using submit button we need to define strongly typed control so that value can be retrieved in the submit action “index” defined in the form name

@Html.TextBoxFor(model => model.Firstname)

1 comment:

Narendran Namachivayam said...

Helpful information to learn MVC