What is updatemodel

In my last article I explained that how we can fetch data from view to controller using model . In my last article I fetched the data from view to controller using model and save this data into database. I am using the same example in this article.

We can also use UpdateModel method to fetch data from view to controller. UpdateModel is the method which is generics type and takes parameter of model type. This method fills the value to the property of this model class.

Code of Controller

		
		
	using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        //use of updateModel method to fetch data from view to controller
        public ActionResult InsertResult()
        {
            MvcApplication1.Models.TechAltum data = new Models.TechAltum();
            UpdateModel(data);
          
            //call insertion method
              int res = data.Insert();
                if (res > 0)
                {

                    Response.Write("Insert Successfully");
                  
                }
                else
                {

                    return RedirectToAction("index", "home");

                }

                return View();
            
        }
    }
}

		

In this example in InsertResult method I simply create the object of model class and pass this object to the UpdateModel method.

Code of Index View

The index view code will be remaining same which is as follows:-

		
		
	@model MvcApplication1.Models.TechAltum

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>

    @using (Html.BeginForm("InsertResult","Home"))
    { 
    
        
        <table>
            <tr>
                <td>Enter First Name</td>
                <td>@Html.TextBoxFor(x=>x.fName)</td>
            </tr>
             <tr>
                <td>Enter Larst Name</td>
                <td>@Html.TextBoxFor(x=>x.lName)</td>
            </tr>
             <tr>
                <td>Enter Course</td>
                <td>@Html.TextBoxFor(x=>x.course)</td>
            </tr>
            <tr>
                <td>Enter Fees</td>
                <td>@Html.TextBoxFor(x=>x.fees)</td>
            </tr>
            <tr>
                <td>Date of Joining</td>
                <td>@Html.TextBoxFor(x=>x.DOJ)</td>
            </tr>

            <tr>
                <td></td>
                <td>@Html.TextBox("btn_submit", "Insert Data", new { type = "submit" })</td>
            </tr>
        </table>
    
    }
</body>
</html>
	
		

Now execute the code and you will get following output

		
		
		updatemodel in MVC
		Figure 1
		
		
		
		
		updatemodel output in mvc
		Figure 2
		
		

Email Address

For any query you can mail me at info@techaltum.com