Sunday 29 December 2013

Using Model type to get the Values from the FORM POST - ASP.MVC

In this article we are going to see how to get the values from the FROM POST using Model type.

HTML

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset style="width:300px">
        <legend>Employee</legend>
        <br />
        <div class="editor-label">
            @Html.LabelFor(m => m.EmployeeName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(m => m.EmployeeName)
            @Html.ValidationMessageFor(m=>m.EmployeeName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(x => x.Country)
        </div>
        <div class="editor-field">
            @Html.EditorFor(x => x.Country)
            @Html.ValidationMessageFor(x=>x.Country)
        </div>
        <div class="editor-label">
            @Html.LabelFor(x => x.DepartmentId, "Department")
        </div>
        <div class="editor-field">
            @Html.DropDownList("DepartmentId", new List<SelectListItem>(){
    new SelectListItem(){Text="TECH",Value="1"},
    new SelectListItem(){Text="HR",Value="2"},
    new SelectListItem(){Text="Finance",Value="3"}
    }, "Select Department")
    @Html.ValidationMessageFor(x=>x.DepartmentId)
        </div>
        <div class="editor-label">
            @Html.LabelFor(x=>x.Married)
        </div>
        <div class="editor-field">
            @Html.DropDownList("Married",new List<SelectListItem>(){
   new SelectListItem(){Text="Yes",Value="YES"},
   new SelectListItem(){Text="No",Value="NO"}  
   })
   @Html.ValidationMessageFor(x=>x.Married)
        </div>
        <br />
        <input type="submit" value="Create" />
    </fieldset>
}

<br />
@Html.ActionLink("Back to List","Index","Department")



C#


[HttpGet]
        [ActionName("Create")]
        public ActionResult Create_Launch()
        {
            return View();
        }

        
        [HttpPost]
        public ActionResult Create(Employee emp)
        {
            BusLayer bl = new BusLayer();
            bl.Create(emp.EmployeeName, emp.Country, emp.Married, emp.DepartmentId);
            return RedirectToAction("Details", new { id = emp.DepartmentId });
        }


            Below code have employee model object but the value if update by the UpdateModel method.

        [HttpPost]
        [ActionName("Create")]
        public ActionResult Create_Submit()
        {
            BusLayer bl = new BusLayer();
            Employee emp = new Employee();
            TryUpdateModel(emp);

            if (ModelState.IsValid)
            {               
                bl.Create(emp.EmployeeName, emp.Country, emp.Married, emp.DepartmentId);
                return RedirectToAction("Details", new { id = emp.DepartmentId });
            }
            return RedirectToAction("Create"); 
        }




From this article you can learn the various ways to get the value of form values from the model type.

No comments:

Post a Comment