using model binding in asp.net mvc

Model binding is very power full technique in Asp.net MVC. You can create a whole view based on your Model.

Create a new project in visual studio and make a simple ActionController name it pubs and its corresponding view

Now you need to connect your Asp.net MVC application to database. I use Entity framework code first approachto create database. See this articleto learn how we connect Asp.net MVC application with database using entity framework database first approach

pubs View Code

@model MultipleFileUploads.Models.Product
@{
    ViewBag.Title = "Product View";
}

<div class="col-lg-12">
    @using (Html.BeginForm("AddNewProduct", "Pubs", FormMethod.Post))
    {
        <label>Enter Product Name</label>
        <input type="text" class="form-control" name="ProductName" />
        <label>Enter Product Price</label>
        <input type="number" class="form-control" name="ProductPrice" />
        <label>Enter Product Comments</label>
        <textarea class="form-control" name="ProductComments"></textarea>
        <label>Select product publish Date</label>
        <input type="date" class="form-control" name="PublishDate" />
		<br />
        <input type="submit" value="Save Author info" class="btn-success" />

    }
</div>

Very first line of the pubs view define the type of your view. this pubs view is strongly type view based on product Model. As you can see i put name attribute in each HTML control. This name is same according to your property define in class. Product class contains same properties. This form post on AddNewProduct action result define in pubs controller. Now we need write our AddNewProduct controller to insert new product in database.

AddNewProduct Controller Code

        public ActionResult AddNewProduct(Product productobj)
        {
            if (ModelState.IsValid)
            {
                 using (Contaxt dbcontext=new Contaxt())
            {
                dbcontext.products.Add(productobj);
                if (dbcontext.SaveChanges() > 0)
                {
                    return View("success");
                }
                else
                {
                    return View("failure");
                }
            }

            } 
            else
            {
                return View("failure");
            }
           
            
        }

AddNewProduct ActionResult receive a product type object. when you post your form this object automatically maps object properties with form properties.

Complete Product form and submit

Model binding in asp.net mvc

Debugger view

Model binding in asp.net mvc