Saturday, 29 July 2017

INSERT DATA IN DATABASE USING MVC


CREATE CLASS FILE IN MODEL:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Testmvcapp.Models
{
    public class ClsUser
    {
        public int MobileID { getset; }

       
        public string User_Id { getset; }

        [Required(ErrorMessage = "Please Enter UserName")]
        [Display(Name = "Enter UserName")]
        [MaxLength(50, ErrorMessage = "Exceeding Limit")]
        public string User_Name { getset; }

        [Required(ErrorMessage = "Please Enter Password")]
        [Display(Name = "Enter Password")]
        [MaxLength(50, ErrorMessage = "Exceeding Limit")]
        public string User_Password { getset; }

        [Required(ErrorMessage = "Please Enter Confirm Password")]
        [Display(Name = "Enter Confirm Password")]
        [Compare("User_Password", ErrorMessage = "Password and Confirmation Password must match.")]
        [MaxLength(50, ErrorMessage = "Exceeding Limit")]
        public string User_ConfPassword { getset; }

        [Required(ErrorMessage = "Please Enter Name")]
        [Display(Name = "Enter Name")]
        [DataType(DataType.Text)]
        public string User_FullName { getset; }

    }
}
ADD DEFAULTCONTROLLER IN CONTROLLER:
  Database2Entities ob = new Database2Entities();

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(ClsUser us)
        {
            if (ModelState.IsValid) 
            {
                TblUser user = new TblUser();
                user.User_FullName = us.User_FullName;
                user.User_Name = us.User_Name;
                user.User_Password = us.User_Password;
                user.User_Code = 123;
                ob.TblUsers.Add(user);
                ob.SaveChanges();
                return View("Index");
            }
            else
            {
                return View();
            }

        }
Create View Page in View:
@model Testmvcapp.Models.ClsUser
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="@Url.Content("~/Content/Gridmvc.css")" rel="stylesheet" />
    <link href="@Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" />
    <script src="@Url.Content("~/Scripts/gridmvc.min.js")"></script>  
    <script src="~/Scripts/jquery-3.1.1.js"></script>
    <script src="~/Scripts/bootstrap.js"></script>
    <script src="~/Scripts/jquery-3.1.1.min.js"></script>
</head>
<body style="background-image:url(../Images/img11.jpg);background-repeat:no-repeat;background-size:cover;">
   @using (Html.BeginForm())
{
        <div style="padding-left:20%;padding-right:20%;padding-top:10%">
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">
                <div class="col-md-3 col-lg-3 col-sm-3 col-xs-3">
                    Enter Name
                </div>
                <div class="col-md-9 col-lg-9 col-sm-9 col-xs-9">
                    @Html.TextBoxFor(a => a.User_FullName, new { @class = "text-primary", width = "100%" })
                    <span style="color:red">
                        @Html.ValidationMessageFor(a => a.User_FullName)
                    </span>
                   
                </div>
            </div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12" style="height:5px;"></div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">
                <div class="col-md-3 col-lg-3 col-sm-3 col-xs-3">
                    User Name
                </div>
                <div class="col-md-9 col-lg-9 col-sm-9 col-xs-9">
                    @Html.TextBoxFor(a => a.User_Name, new { @class = "text-primary", width = "100%" })
                    <span style="color:red">
                        @Html.ValidationMessageFor(a => a.User_Name)
                    </span>
                    
                </div>
            </div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12" style="height:5px;"></div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">
                <div class="col-md-3 col-lg-3 col-sm-3 col-xs-3">
                    Enter Password
                </div>
                <div class="col-md-9 col-lg-9 col-sm-9 col-xs-9">

                    @Html.TextBoxFor(a => a.User_Password, new { @class = "text-primary", width = "100%", type = "password" })
                    <span style="color:red">
                        @Html.ValidationMessageFor(a => a.User_Password)
                    </span>
                    @*<input type="text" id="txtpassword" style="width:280px;" class="text-primary" />*@
                </div>
            </div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12" style="height:5px;"></div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">
                <div class="col-md-3 col-lg-3 col-sm-3 col-xs-3">
                    ReEnter Password
                </div>
                <div class="col-md-9 col-lg-9 col-sm-9 col-xs-9">
                    @Html.TextBoxFor(a => a.User_ConfPassword, new { @class = "text-primary", width = "100%", type = "password" })
                    <span style="color:red">
                        @Html.ValidationMessageFor(a => a.User_ConfPassword)
                    </span>

                   
                </div>
            </div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12" style="height:5px;"></div>
            
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12" style="height:5px;"></div>
            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">
                <div class="col-md-3 col-lg-3 col-sm-3 col-xs-3">

                </div>
                <div class="col-md-9 col-lg-9 col-sm-9 col-xs-9">
                    <input type="submit" value="Upload" class="btn btn-primary" onclick="location.href='@Url.Action("Index""DefaultController")'" />
                  
                </div>

            </div>
        </div>
   }
</body>
</html>




No comments:

Post a Comment