INSERT DATA IN DATABASE USING HTML CONTROLS IN MVC

For inserting data in Database you have to follow the below methods. Here we also give the validation on HTML controls. MVC stand for Model, View, Controller. First we have to create a Model According the table parameters.

Now create a class file in Model folder for to carry the table parameters and with its respective validation. Follow the below Example... 

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 { get; set; }
public string User_Id { get; set; }
[Required(ErrorMessage = "Please Enter UserName")]
[Display(Name = "Enter UserName")]
[MaxLength(50, ErrorMessage = "Exceeding Limit")]
public string User_Name { get; set; }
[Required(ErrorMessage = "Please Enter Password")]
[Display(Name = "Enter Password")]
[MaxLength(50, ErrorMessage = "Exceeding Limit")]
public string User_Password { get; set; }
[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 { get; set; }
[Required(ErrorMessage = "Please Enter Name")]
[Display(Name = "Enter Name")]
[DataType(DataType.Text)]
public string User_FullName { get; set; }
}
}
After create the Model Class, Add controller in Controller folder for to write action methods. Follow the below Example...

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();


}






}





Then add a View page from controller ActionResult. Follow the below Example...






@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>

In this way we post the records in database.

Related Links:





Post a Comment

0 Comments