How to Insert Data in MVC Razor View Engine in Asp.Net

                             MVC  INSERT




DEMO

Create - table Name (register)

CREATE TABLE [dbo].[register] (
    [userid]   INT          IDENTITY (1, 1) NOT NULL,
    [username] VARCHAR (50) NOT NULL,
    [password] VARCHAR (50) NOT NULL,
    [name]     VARCHAR (50) NOT NULL,
    [mobile]   VARCHAR (50) NOT NULL,
    [email]    VARCHAR (50) NOT NULL,
    PRIMARY KEY CLUSTERED ([userid] ASC)


);


Coding for Validation


 using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
   
    public partial class register
    {
        public int userid { getset; }

        [Required(ErrorMessage = "Please Enter UserName", AllowEmptyStrings = false)]

        public string username { getset; }

        [Required(ErrorMessage = "Please Enter Valid Password", AllowEmptyStrings = false)]
        [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
        [StringLength(50, MinimumLength = 15, ErrorMessage = "please Password Lenth 15 Long")]

        public string password { getset; }


        [Required(ErrorMessage = "Please  Enter Full name", AllowEmptyStrings = false)]

        public string name { getset; }
        [Required]
    
        public string mobile { getset; }

        [Required]

        [RegularExpression("^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$", ErrorMessage = "Invalid Email ID")]

        public string email { getset; }
    }
}


Coding for Insert


  public ActionResult Register()
        {
            return View();
        }
       
        [HttpPost]
     
  [ValidateAntiForgeryToken]      

        public ActionResult register(register r)

        {
            if (ModelState.IsValid)
            {
                using (Database1Entities de = new Database1Entities())
                {
                    de.registers.Add(r);

                    de.SaveChanges();

                    ModelState.Clear();

                    r = null;

         ViewBag.Message = "Data Registered Successfully";              
                               
                }
             
            }

            return View(r);
      
        }


CSHTML Coding


@model MVC__REGISTRATION.register

@{
    ViewBag.Title = "register";
}

<h2>register</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>register</legend>

         @Html.AntiForgeryToken()


        <div class="editor-label">
            @Html.LabelFor(model => model.userid)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.userid)
            @Html.ValidationMessageFor(model => model.userid)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.username)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.username)
            @Html.ValidationMessageFor(model => model.username)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.password)
            @Html.ValidationMessageFor(model => model.password)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.name)
            @Html.ValidationMessageFor(model => model.name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.mobile)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.mobile)
            @Html.ValidationMessageFor(model => model.mobile)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.email)
            @Html.ValidationMessageFor(model => model.email)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

}




Learn About ASP.NET MVC

ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that enables a clean separation of concerns and that gives you full control over markup for enjoyable, agile development. 


ASP.NET is a development framework for building web pages and web sites with HTML, CSS, JavaScript and server scripting.

ASP.NET supports three different development models:
Web Pages, MVC (Model View Controller), and Web Forms.



Web Applications with 3 logic layers:

The business layer (Model logic)
The display layer (View logic)
The input control (Controller logic)


MVC




First  Select - New Project 







Next  - Select  FrameWork  4 or 4.5  -  Select  Asp.Net MVC  Web  Application  3 or 4 -  Enter the Project  Name -  Click OK Button 








Next  - Open Window  Form - Select -  Internet Application

Next  - Select - Razor View Engine







 
Next  - Open the HomeController.cs Below like that










Next  - Right Click App-Data - Add- New Item 










Add -  SQL Server Database -  Add










Added The Database To App-Data Folder








Next  - Add the New Table Name - Select Tables - Right Click - Add New Table 









Create table Name (register)





After  Filling  Column name  - Select  Update Button Click 








Next - Select - Update Database Click 









Add - The register Table & Column Fields









Next - Select  - Solution Explorer - Right Click - Project Name(MVC REGISTRATION) -  Add - New Item









   Add the ADO.NET Entity Model -  Enter model - Name- OK      Button Click








Next -  Show Window -  Select - Generate from Database - Click Next Button 







Open New Window - Select - Your Databsae Name -  Database1Entities - Click  Next Button









Next  - Select  Your  Table Name(register)









Next - Create  Model.edmx  diagram1  (register table & Column Fields)









Next  - Create   model  for register table - Next Double click - register.cs - open  register table fields  &  Datatypes 








Next -Add - the  using System.ComponentModel.DataAnnotations;   for  Validation










Next -   Add the Validation for  All   register table Column fields  below like that









Next - Select  Controller - right click - Add - Controller 







Open window -  Controller Name - Default1Controller  - Template - Empty MVC Controller  - Add  Button click








Next - Added Default1Controllers -  Select  Default1Controller 







Next - Add 

[HttpPost]
        [ValidateAntiForgeryToken]





Next - Write the  Insert  Coding 
















Next  - Select  Build  - Right click - Select Build Solution  - Build the  Controller 










Next  - Right Click  -  Coding Page - Public Actionresult  Register() -  Add View  Click










Open - New Window -   Select - View(Register)-View Engine(Razor(CSHTML))- Select Strongly typed View - Model Class(register(MVC_Registration)) -Scaffold Tempalte(Empty) - Add Button Click 









Default1 View Name Generate

Next  - Automatically Generate -register.CSHTML Coding for Register  Column field Design 


Next  - Add @ Html.AntiForgerytoken()




                                           
              
                             
                                     Default1  View Name 










Next - run the program - Clcik   - F5 Buttton  - Go to Browser 








Next -  Add - the  View Name /  Controller Name(Default1/register)

Next - open  the Generated  Design  Page 










Next -  Without Fill Values  Validation Error Message  Show popup









All - Validation  Error Message Popup Shown  below 











Next - Password Strength  15 Given   Validation  Popup Show 









Next - Enter  - All Values -  Properly - Click Create Button 
      
      Registered Successfully  









Next - Enter  - Second  Values -  Properly - Click Create Button 

Registered Successfully









Next  - Open Table  - Select  Register  table  Right Click - Show table data -  Display the  Registered Data's 






0 comments:

Post a Comment