Wednesday 8 October 2014

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

1 comment    
categories: , ,
MVC  INSERT


DEMO

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.
MVC









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 { get; set; }

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

        public string username { get; set; }

        [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 { get; set; }


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

        public string name { get; set; }
        [Required]
    
        public string mobile { get; set; }

        [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 { get; set; }
    }
}


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")

}






































                                                                                                      











i think useful this post. 

1 comment:

  1. Great Content. It will useful for knowledge seekers. Keep sharing your knowledge through this kind of article.
    MVC Training in Chennai
    MVC Course in Chennai

    ReplyDelete