Click here to Skip to main content
15,881,803 members
Articles / Web Development / ASP.NET

ASP.NET MVC Model Binding - Part1

Rate me:
Please Sign up or sign in to vote.
4.45/5 (22 votes)
21 Feb 2011CPOL4 min read 145.2K   2.2K   50  
This article gives the reader detailed understanding on how ASP.NET MVC Model binding works.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;



using ModelBinding1.Models ;

namespace ModelBinding1.Controllers
{
    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/

        public ActionResult Index()
        {

            return View("EmployeeView1");
            
        }
        /// <summary>
        /// Query string Parameter Model binding
        /// </summary>
        /// <param name="employee"></param>
        /// <param name="EmpId"></param>
        /// <param name="EmpName"></param>
        /// <param name="EmpDepartment"></param>
        /// <param name="EmpDesignation"></param>
        /// <returns></returns>
        public ActionResult UpdateQueryString(int EmpId, string EmpName, string EmpDepartment, string EmpDesignation)
        {
            return View("EmployeeView1");

        }
        /// <summary>
        /// Controlling the Model Binding using Bind Attribute. 
        /// </summary>
        /// <param name="employee"></param>
        /// <returns></returns>

        public ActionResult BindInclude([Bind(Include = "EmpId,EmpName", Exclude = "EmpSalary")] Employee employee)
        {
            return View("EmployeeView1");
        }






        

        /// <summary>
        /// object Model binding
        /// </summary>
        /// <param name="employee"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Update(Employee employee )
        {
            if (!this.ModelState.IsValid)

                ModelState.AddModelError("Employee", "Employee Not Valid");
            return View("EmployeeView1");
        }

        [HttpPost]
        public ActionResult UsingPrefix([Bind(Prefix = "employee")]Employee employee)
        {
            if (!this.ModelState.IsValid)

                ModelState.AddModelError("Employee", "Employee Not Valid");
            return View("EmployeeView2");
        }

        public ActionResult EmployeeView2()
        {
            return View("EmployeeView2");
        }




    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
I have around 9 years of experience in Microsoft technologies, .Net 2.0,3.5, Asp.net MVC developed small to medium scale products using Silverlight 2.0, Asp.net Ajax technologie and Javascript frameworks.

Comments and Discussions