Introduction:
Here in this post I will show you how to make a form and how to save the inputted value of the form into database and how to validate your textbox before saving the data using MVC4.0 using Razor view engine.
So first create or choose an existing database and create a table named MVC_Details. Give any name to table .
Now follow the some more Images below to know how to Validate your Textbox fields before Insertion
In Controller Class you have to call the method IsValid with refrence variable ModelState in if else condition like ..
Here in this post I will show you how to make a form and how to save the inputted value of the form into database and how to validate your textbox before saving the data using MVC4.0 using Razor view engine.
So first create or choose an existing database and create a table named MVC_Details. Give any name to table .
Now start a MVC project and choose Razor as a view engine.
Create a Class in Model Folder whose name is ColumnSetGet.cs (give any name according to your project).In this class you just provide all the column name with set and get method.This class will reflect the table columns.
After creation of set and get method in a class , now you have to create one another class in Model folder whose name is InsertMVCModel.cs ( give any name according to you ). In this class you jsut write the SQL Query to Insert, Update ,Delete aur any SQL Query to interact with Database.
Now do some coding work in Controller .
So create a Controller class within the Controller folder named InsertController( give any name according to you ).Add the namespace using PracticeMVC.Models; [ PracticeMVC is my project name and Model is a Model folder name, give your project name.model in controller class ] for accessing the model folder into your Controller class. If you put the classes in any other folder add that folder name as namespace.
I took InsertView as the ActionMethod, so there will be two Action method named InsertView. The first one for just showing the form and second one is for getting the value after submit button click. We don't need to write any code within the first method, But in the second method write down the code.
using PracticeMVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace PracticeMVC.Controllers
{
public class InsertController : Controller
{
public ActionResult InsertView()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult InsertView(FormCollection frm)
{
ColumnSetGet csg = new ColumnSetGet();
if (string.IsNullOrEmpty(frm["Name"]))
{
ModelState.AddModelError("Name", "Enter Your
Name");
if (string.IsNullOrEmpty(frm["Msg"]))
{
ModelState.AddModelError("Msg", "Enter Your
Message");
}
}
else if (ModelState.IsValid)
{
csg.Name = frm["Name"];
csg.Msg = frm["Msg"];
InsertMVCModel inm = new InsertMVCModel();
if (inm.AddDetails(csg) == 1)
{
ViewBag.Status = "Success!!";
}
}
return View("InsertView");
}
}
}
Now create the View part. To create the View right_click on the Action Method and keep the name as it is. Just one change you have to do. Check the Create Strongly typed view and choose ColumnSetGet as your Modal class. Now click on the Add button to make your View.
In the view we have to make form first. To create a form we have to use Html.BeginForm() and within that you have to put labels, textboxes and buttons.
So lets create our own form.
Now your form is ready to Insert data into Database .
Now follow the some more Images below to know how to Validate your Textbox fields before Insertion
In Controller Class you have to call the method IsValid with refrence variable ModelState in if else condition like ..
// If TextBox is not Empty
if(ModelState.IsValid)
if(ModelState.IsValid)
{
// your code;
}
// If TextBox is Empty
else if(string.IsNullOrEmpty(frm["Name"]))
{
ModelState.AddModelError("Name", "Enter Your Name");
}
for more see the Image below....
Now in View Page add One Attribute @Html.ValidationSummary()
Now Your Form is ready with Insertion SQL Query with Textbox Validation .Run your program

























