Context is folder name Where ADO.net Entity model are store.
In the server Name Prese .(DOT) then Database name automatically in the dropdwonlist
Add caption |
Step 4 Create a new Empty Controller.
Create a Empty controller name Student
1. Insert Code
View Design Code :
@model CRUDE.Context.tbl_student
@{
ViewBag.Title = "Student";
}
<h2>Student</h2>
<style>
.error{
color:red;
}
</style>
@using (Html.BeginForm("AddStudent", "Student", FormMethod.Post)) // that is rezor syntax
{
<div class="container">
<div class="form-group">
<label>Name</label>
@Html.TextBoxFor(x => x.Name, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Name,"", new {@class="error" })
</div>
<div class="form-group">
<label>Father Name</label>
@Html.ValidationMessageFor(x => x.Fname, "", new { @class = "error" })
@Html.TextBoxFor(x => x.Fname, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Email</label>
@Html.ValidationMessageFor(x => x.Email, "", new { @class = "error" })
@Html.TextBoxFor(x => x.Email, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Mobile</label>
@Html.ValidationMessageFor(x => x.Mobile, "", new { @class = "error" })
@Html.TextBoxFor(x => x.Mobile, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Description</label>
@Html.ValidationMessageFor(x => x.Description, "", new { @class = "error" })
@Html.TextAreaFor(x => x.Description, new { @class = "form-control", @rows = "10" })
</div>
<div class="form-group">
<button type="submit" class=" btn-primary">Submit</button>
</div>
</div>
}
Controller Code Add Student record in the database
using CRUDE.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CRUDE.Controllers
{
public class StudentController : Controller
{
DB_Crud_testEntities dbobj = new DB_Crud_testEntities();
public ActionResult Student()
{
return View();
}
// mvcaction4 prese Tab
[HttpPost]
public ActionResult AddStudent(tbl_student model)
{
if (ModelState.IsValid)
{
tbl_student obj = new tbl_student();
obj.Name = model.Name;
obj.Fname = model.Fname;
obj.Email = model.Email;
obj.Mobile = model.Mobile;
obj.Description = model.Description;
dbobj.tbl_student.Add(obj);
dbobj.SaveChanges();
}
ModelState.Clear();
return View("Student");
}
}
}
Model :
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace CRUDE.Context
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class tbl_student
{
public int ID { get; set; }
[Required(ErrorMessage ="Please select Name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please select father Name")]
public string Fname { get; set; }
[Required(ErrorMessage = "Please select email Name")]
[EmailAddress]
public string Email { get; set; }
[Required(ErrorMessage = "Please select Mobile Number")]
[MinLength(11,ErrorMessage ="Mobile Number should be 11 digit")]
public string Mobile { get; set; }
[Required(ErrorMessage = "Please select Description")]
public string Description { get; set; }
}
}
No comments:
Post a Comment