Sunday, 4 June 2017

Submit (Post) Form using Action Link in Asp.Net MVC Razor

Submit (Post) Form using Action Link in Asp.Net MVC Razor
Description:
In this example we explain that how to submit Form using Action Link in Asp.Net MVC Razor or how to Post Form using Action Link in MVC Razor view. @Html.ActionLink is rendered as an Html Anchor or Hyperlink tag.

So here we demonstrate that how to submit or post form using ActionLink in Asp.Net MVC 5 razor.
Model:

public class EmployeeModel
{
    ///<summary>
    /// Gets or sets EmployeeId.
    ///</summary>
    public int EmployeeId { getset; }

    ///<summary>
    /// Gets or sets Name.
    ///</summary>
    public string Name { getset; }

    ///<summary>
    /// Gets or sets Gender.
    ///</summary>
    public string Gender { getset; }

    ///<summary>
    /// Gets or sets City.
    ///</summary>
    public string City { getset; }
}

Controller:
public class EmployeeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(EmployeeModel employee)
    {
        int EmployeeId = employee.EmployeeId;
        string name = employee.Name;
        string gender = employee.Gender;
        string city = employee.City;

        return View();
    }
}

View:

@model ActionLink_Send_Model_MVC.Models.EmployeeModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index""Employee"FormMethod.Post))
    {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center">Employee Details</th>
            </tr>
            <tr>
                <td>EmployeeId: </td>
                <td>
                    @Html.TextBoxFor(m => m.EmployeeId)
                </td>
            </tr>
            <tr>
                <td>Name: </td>
                <td>
                    @Html.TextBoxFor(m => m.Name)
                </td>
            </tr>
            <tr>
                <td>Gender: </td>
                <td>
                    @Html.DropDownListFor(m => m.Gender, new List<SelectListItem>
                   { new SelectListItem{Text="Male", Value="M"},
                     new SelectListItem{Text="Female", Value="F"}}, "Please select")
                </td>
            </tr>
            <tr>
                <td>City: </td>
                <td>
                    @Html.TextBoxFor(m => m.City)
                </td>
            </tr>
            <tr>
                <td></td>
                <td>@Html.ActionLink("Submit"""nullnew { @id = "submit" })</td>
            </tr>
        </table>
    }
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#submit").click(function () {
                document.forms[0].submit();
                return false;
            });
        });
    </script>
</body>
</html>


This entry was posted in :

0 comments:

Post a Comment