articles

Home / DeveloperSection / Articles / Creating a Simple Model Using ASP.NET MVC4

Creating a Simple Model Using ASP.NET MVC4

Vijay Shukla9679 26-Nov-2012

In this article, I will look to create a simple customerInformation model using ASP.NET MVC3.

Here is I have two Views, one (Index.cshtml) is for enter data in


CustomerInformation Model and second (ShowData.cshtml) is for show the


CustomerInformation Model data.


Steps for creating a new Application in ASP.NET MVC4


1.       Open studio 2010 and create a new ASP.NET MVC 4 Web Application (Razor) project as seen below:

Creating a Simple Model Using ASP.NET MVC4

Select the Empty Template and then select the Razor in View Engine Drop Down ListBox Then Press Ok:

Creating a Simple Model Using ASP.NET MVC4

 Add a controller in your project and give name as HomeController as shown in the below figure:

 

Creating a Simple Model Using ASP.NET MVC4

 

4.   Add a Model Class in your project and give name as CustomerInformation as


shown in the below figure:

 

Creating a Simple Model Using ASP.NET MVC4

 

5.   Add two Views in the project first view with the Name Index it’s create with


Strongly type and second view with the name ShowData and it’s also create


Strogly type.

 

Index.cshtml
@model CustomerInformationMVC3.Models.CustomerInformation

@{
    ViewBag.Title = "Index";
}
@*<script type="text/javascript">
    function s() {
        location.href = "/Home/ShowData"
    }
</script>*@
@using (Html.BeginForm("ShowData", "Home"))
{
  @Html.Label("Customer Name:: ");
  @Html.TextBoxFor(m => m.customer_Name)<br />
  @Html.Label("Customer Age:: ");
   @Html.TextBoxFor(m => m.customer_Age)<br />
   @Html.Label("Customer Contact:: ");
   @Html.TextBoxFor(m => m.customer_Contact)<br />
   @Html.Label("Customer Address:: ");
   @Html.TextBoxFor(m => m.customer_Address)<br />
  <input type="submit" value="Submit" />
}

 ShowData.cshtml


@model CustomerInformationMVC3.Models.CustomerInformation

@{
    ViewBag.Title = "ShowData";
}

<h2>ShowData</h2>

Customer Name&nbsp @Model.customer_Name <br />
Customer Age&nbsp @Model.customer_Age <br />
Customer Contact&nbsp @Model.customer_Contact <br />
Customer Address&nbsp @Model.customer_Address <br />

 

HomeController
using System;

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

namespace CustomerInformationMVC3.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {

            return View();
        }
        public ActionResult ShowData(CustomerInformation _customerInformation)
        {
            return View(_customerInformation);
        }
    }
}

  Model Class CustomerInformation


using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace CustomerInformationMVC3.Models
{
    public class CustomerInformation
    {
        public string customer_Name{ get; set; }
        public string customer_Address{ get; set; }
        public int customer_Age{ get; set; }
        public string customer_Contact{ get; set; }
    }
}
 

 

Run this application, you will see the following screen:

Creating a Simple Model Using ASP.NET MVC4

 You can enter the customer Name, Age, Contact, Address and press Submit

After press Submit Button , you will see the following screen:

 

Creating a Simple Model Using ASP.NET MVC4

 

 


Updated 07-Sep-2019

Leave Comment

Comments

Liked By