Controller-->TestController.cs
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using MvcApplication.Models;
namespace MvcApplication.Controllers
{
public class TestController : Controller
{
//
// GET: /Test/
private readonly MvcDbContext _db;
private string _opeartionName;
private int _id;
private string _name;
public TestController()
{
_db = new MvcDbContext();
}
public ActionResult TestIndex()
{
return View("~/Views/Test/TestIndexView.cshtml",GetValues());
}
public ActionResult CreateTestIndex()
{
return View("~/Views/Test/TestIndexCreateView.cshtml");
}
[HttpPost]
public ActionResult CreateTestIndex(TestModel testModel)
{
_db.Users.Add(new User { Id = testModel.Id, Name = testModel.Name });
_db.SaveChanges();
return View("~/Views/Test/TestIndexView.cshtml", GetValues());
}
public TestViewModel GetValues()
{
var testViewModel = new TestViewModel { Users = (List<User>)ReadFromDb.GetValuesFromDb() };
return testViewModel;
}
public ActionResult EditTestIndex(string id, string name)
{
var testModel = new TestModel {Id = Convert.ToInt32(id), Name = name};
return View("~/Views/Test/EditTestIndexView.cshtml", testModel);
}
public ActionResult UpdateTestIndex(TestModel testModel)
{
_opeartionName = "Update";
_id = testModel.Id;
_name = testModel.Name;
ReadFromDb.GetUpdateDeleteValues(_opeartionName, _id, _name);
return View("~/Views/Test/TestIndexView.cshtml",GetValues());
}
public ActionResult DeleteTestIndex(int id)
{
_opeartionName = "Delete";
_id = id;
_name = string.Empty;
ReadFromDb.GetUpdateDeleteValues(_opeartionName, _id, _name);
return View("~/Views/Test/TestIndexView.cshtml",GetValues());
}
}
}
Controller-->TestModel.cs
namespace MvcApplication.Controllers
{
public class TestModel
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Controller-->TestViewModel.cs
using System.Collections.Generic;
using MvcApplication.Models;
namespace MvcApplication.Controllers
{
public class TestViewModel
{
public List<User> Users { get; set; }
}
}
Model-->User.cs
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Data.SqlClient;
namespace MvcApplication.Models
{
public static class ReadFromDb
{
private static List<User> _userList;
private static string GetConnection()
{
return ConfigurationManager.ConnectionStrings["DataConnectionString"].ConnectionString;
}
public static IList<User> GetValuesFromDb()
{
_userList = new List<User>();
using (var con = new SqlConnection(GetConnection()))
{
con.Open();
using (var command = new SqlCommand("SELECT * FROM Users", con))
{
var reader = command.ExecuteReader();
while (reader.Read())
{
var id = (int)reader[0];
var name = reader[1] as string;
_userList.Add(new User { Id = id, Name = name });
}
}
con.Close();
}
return _userList;
}
public static void GetUpdateDeleteValues(string operationName, int identity, string editedName)
{
SqlConnection sqlConnection;
SqlCommand sqlCommand;
switch (operationName)
{
case "Update":
if (!string.IsNullOrEmpty(editedName) && identity != 0)
{
using (sqlConnection = new SqlConnection(GetConnection()))
{
sqlConnection.Open();
using (sqlCommand = new SqlCommand(string.Format("update Users set Name='{0}' where Id={1}", editedName, identity), sqlConnection))
{
sqlCommand.ExecuteReader();
}
}
}
break;
case "Delete":
if (identity != 0 && string.IsNullOrEmpty(editedName))
{
using (sqlConnection = new SqlConnection(GetConnection()))
{
sqlConnection.Open();
using (sqlCommand = new SqlCommand(string.Format("delete from Users where Id={0}", identity), sqlConnection))
{
sqlCommand.ExecuteReader();
}
}
}
break;
}
}
}
public class MvcDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public MvcDbContext()
: base("DataConnectionString")
{
}
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Views-->Test--->TestIndexView.cshtml
@model MvcApplication.Controllers.TestViewModel
@{
ViewBag.Title = "TestIndexView";
}
<h2>TestIndexView</h2>
<table>
<thead>
<tr>
<td>ID</td>
<td>NAME</td>
<td> </td>
</tr>
</thead>
<tbody>
@foreach (var temp in Model.Users)
{
<tr>
<td>
@temp.Id
</td>
<td>
@temp.Name
</td>
<td>
@Html.ActionLink("Edit", "EditTestIndex", "Test", new { id = temp.Id, name = temp.Name }, null)
</td>
<td>
@Html.ActionLink("Delete","DeleteTestIndex","Test",new{id=temp.Id},null)
</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td>
@Html.ActionLink("Create", "CreateTestIndex", "Test", null, null)
</td>
</tr>
</tfoot>
</table>
Views-->Test--->TestIndexCreateView.cshtml
@model MvcApplication.Controllers.TestModel
@{
ViewBag.Title = "TestIndexCreateView";
}
<h2>TestIndexCreateView</h2>
@using (Html.BeginForm())
{
@Html.Label("Name")
@Html.TextBoxFor(model => model.Name)
<br/>
<input type="submit" value="Save"/>
@Html.ActionLink("Cancel","TestIndex","Test",null,null)
}
Views-->Test--->EditTestIndexView.cshtml
@model MvcApplication.Controllers.TestModel
@{
ViewBag.Title = "UpdateTestIndex";
}
<h2>EditTestIndex</h2>
@using (Html.BeginForm("UpdateTestIndex", "Test", FormMethod.Get))
{
@Html.HiddenFor(model=>model.Id)
@Html.Label("Name")
@Html.TextBoxFor(model=>model.Name)
<br/>
<input type="submit" value="Update" />
@Html.ActionLink("Cancel","TestIndex","Test",null,null)
}
Web.Config
<connectionStrings>
<add name="DataConnectionString" connectionString="server=YourServerName;database=TestedDB;User Id=YourUserName;Password=YourPassword;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
</connectionStrings>
OutPuts:
Comments
Post a Comment