MVC Application


MVC Structure:                                                                 Application Structure:
 


  • Create New MVC Project 
  • Add New Folder DataAccessLayer and Create Class DBcode.cs


DBcode.cs



using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using SMS.Models;
using System.Data;

namespace SMS.DLayer
{
    public class DBcode
    {
       
        public SqlConnection Con()
        {
            SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ToString());
            return Cn;
        }

        public string Insert(StdModel StdCls)
        {
            SqlConnection cn = Con();
           
            SqlCommand cmd = new SqlCommand("SP_SMS",cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@Opt", StdCls.Opt);
            cmd.Parameters.AddWithValue("@RollNo", StdCls.RollNo);
            cmd.Parameters.AddWithValue("@StudentName", StdCls.StudentName);
            cmd.Parameters.AddWithValue("@Religion", StdCls.Religion);
            cmd.Parameters.AddWithValue("@Age", StdCls.Age);
            cmd.Parameters.AddWithValue("@Class", StdCls.Class);
            cmd.Parameters.AddWithValue("@FatherName", StdCls.FatherName);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();

            return "";


        }

        public DataTable Select(StdModel StdCls)
        {
            SqlConnection cn = Con();
            SqlCommand cmd = new SqlCommand("SP_SMS",cn);
            cmd.CommandType = CommandType.StoredProcedure;

            DataTable dt = new DataTable();

            cmd.Parameters.AddWithValue("@Opt", StdCls.Opt);
            cn.Open();
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;

            da.Fill(dt);
            cn.Close();

            return dt;
         }


        public string Delete(StdModel StdCls)
        {
            SqlConnection cn = Con();
            SqlCommand cmd = new SqlCommand("SP_SMS", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@Opt",StdCls.Opt);
            cmd.Parameters.AddWithValue("@StdID", StdCls.StdID);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();

            return "";

        }  
    }
}




StdModel.cs


Create New Model Class.


using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace SMS.Models
{
    public class StdModel
    {
        public int Opt { get; set; }
        public int StdID { get; set; }
        [Required(ErrorMessage="Please Enter RollNo")]
        public string RollNo { get; set; }


        [Required(ErrorMessage = "Please Enter StudentName")]
        public String StudentName { get; set; }
       
        [Required(ErrorMessage = "Please Enter Religion")]
        public string Religion { get; set; }


        [Required(ErrorMessage = "Please Enter Age")]
        public int Age { get; set; }

        [Required(ErrorMessage = "Please Enter Class")]
        public string Class { get; set; }

        [Required(ErrorMessage = "Please Enter FatherName")]
        public string FatherName { get; set; }

        public List<GridStdModel> ListStdModel = new List<GridStdModel>();

    }


    public class GridStdModel
    {
        public int Opt { get; set; }
        public int StdID { get; set; }
        public string RollNo { get; set; }
        public String StudentName { get; set; }
        public string Religion { get; set; }
        public int Age { get; set; }
        public string Class { get; set; }
        public string FatherName { get; set; }
    }
}




StudentEntryController.cs

     Create New Controller for Student Management Application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SMS.DLayer;
using SMS.Models;
using System.Data;

namespace SMS.Controllers
{
    public class StudentEntryController : Controller
    {
        //
        // GET: /StudentEntry/
        [HttpGet]
        public ActionResult Student()
        {
            StdModel Std = new StdModel();
            GetRecord(Std);
            return View(Std);
        }


        public void GetRecord(StdModel Std)
        {
            DataTable dt = new DataTable();

            Std.Opt = 2;

            DBcode DbCls = new DBcode();
            dt=DbCls.Select(Std);

            if(dt.Rows.Count>0)
            {
                List<GridStdModel> ListStdModel = new List<GridStdModel>();

                for(int i=0;i<dt.Rows.Count;i++)
                {
                    GridStdModel StdMod = new GridStdModel();
                    StdMod.StdID = Convert.ToInt32(dt.Rows[i]["StdId"]);
                    StdMod.RollNo = Convert.ToString(dt.Rows[i]["RollNo"]);
                    StdMod.StudentName = Convert.ToString(dt.Rows[i]["StudentName"]);
                    StdMod.Religion = Convert.ToString(dt.Rows[i]["Religion"]);
                    StdMod.Age = Convert.ToInt32(dt.Rows[i]["Age"]);
                    StdMod.Class = Convert.ToString(dt.Rows[i]["Class"]);
                    StdMod.FatherName = Convert.ToString(dt.Rows[i]["FatherName"]);

                    ListStdModel.Add(StdMod);
                }
                Std.ListStdModel = ListStdModel;
            }
        }


        [HttpPost]
        public ActionResult Student(StdModel Std)
        {
            DBcode DB = new DBcode();

            if(ModelState.IsValid)
            {
            Std.Opt = 1;

            DB.Insert(Std);
            return RedirectToAction("Student");
            }

            GetRecord(Std);

            return View("Student", Std);
        }

       
        public ActionResult DelStudent(string StdId)
        {
            StdModel Std = new StdModel();
            DBcode DB = new DBcode();

            Std.Opt = 3;
            Std.StdID = Convert.ToInt32(StdId);

            DB.Delete(Std);

            GetRecord(Std);
            return View("Student",Std);
        }
     }
}



View Page:

@model SMS.Models.StdModel
@{
    ViewBag.Title = "Student";
}

<h2>Student Entry</h2>


@using (Html.BeginForm())
{
   
    @Html.ValidationSummary(true)


<table border="0" align="center" width="60%" height="50%">
    <tr>
        <td>RollNo</td>
        <td>@Html.TextBoxFor(m=>m.RollNo)
       
        @Html.ValidationMessageFor(m => m.RollNo, "", new { @style = "Color:Red" })
        </td>
    </tr>

    <tr>
        <td>Student Name</td>
        <td>@Html.TextBoxFor(m=>m.StudentName)
       
        @Html.ValidationMessageFor(m => m.StudentName, "", new { @style = "Color:Red" })
        </td>
    </tr>

    <tr>
        <td>Religion</td>
        <td>@Html.TextBoxFor(m=>m.Religion)
        @Html.ValidationMessageFor(m => m.Religion, "", new { @style = "Color:Red" })
        </td>
    </tr>

    <tr>
        <td>Age</td>
        <td>@Html.TextBoxFor(m=>m.Age)
        @Html.ValidationMessageFor(m => m.Age, "", new { @style = "Color:Red" })
        </td>
    </tr>

    <tr>
        <td>Class</td>
        <td>@Html.TextBoxFor(m=>m.Class)
       
        @Html.ValidationMessageFor(m => m.Class, "", new { @style = "Color:Red" })
        </td>
    </tr>

    <tr>
        <td>Father Name</td>
        <td>@Html.TextBoxFor(m=>m.FatherName)
        @Html.ValidationMessageFor(m => m.FatherName, "", new { @style = "Color:Red" })
       
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center"><input type="submit" formaction="../StudentEntry/Student" value="Save" /></td>

    </tr>

</table>
}
<br />
<br />
<h2>Student Result</h2>


   @if(Model!=null)
   {
       if(Model.ListStdModel!=null)
       {
           var grid = new WebGrid(source: Model.ListStdModel);
          
           @grid.GetHtml(
                columns: new[] {
                   
                   grid.Column("RollNo","Roll No",style:"width:5%; text-align:center;"),
                   grid.Column("StudentName","Student Name",style:"width:20%; text-align:center;"),
                   grid.Column("Religion","Religion",style:"width:20%; text-align:center;"),
                   grid.Column("Age","Age",style:"width:20%; text-align:center;"),
                   grid.Column("Class","Class",style:"width:20%; text-align:center;"),
                   grid.Column("FatherName","FatherName",style:"width:5%; text-align:center;"),
                 
                   grid.Column(format: (x) => Html.ActionLink("Delete", "DelStudent", "StudentEntry", new { StdID=x.StdID },new{onclick="return confirm('Are you sure you want to delete this record?')"}), header:"Delete"),   
               
                });
  
       }
      
   }



Set Start Page Use RouteConfig.cs

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "StudentEntry", action = "Student", id = UrlParameter.Optional }
            );
        }



Page:




Validation 
       In this Section I Am Using DataAnnotations To Validate The Html Controls. 

Set Validations on Model class
               
                    [Required(ErrorMessage="Please Enter RollNo")]
            public string RollNo { get; set; }
    

                    @Html.TextBoxFor(m=>m.RollNo)                                                                 @Html.ValidationMessageFor(m => m.RollNo, ""new { @style = "Color:Red" })
                                   
                     Result:







Comments

Popular posts from this blog

Insecure cookie setting: missing Secure flag

Maximum Stored Procedure Function Trigger or View Nesting Level Exceeded (limit 32) in SQL Server

Display Line Chart Using Chart.js MVC C#