i have following view its working fine till it display code ..but I want when I click on edit button in tabular data it should display selected data in entry section ..like user name in name ,password in password and so on so on
Code:
@model EMS1.Models.UsersViewModel
@{
ViewBag.Title = "List of users";
var grid = new WebGrid(source: Model.users, canPage: true, rowsPerPage: 10);
grid.Pager(WebGridPagerModes.All);
}
@{
ViewBag.Title = "IndexViewModel";
}
<h2>Grid</h2>
<style type="text/css">
.table
{
margin: 4px;
border-collapse: collapse;
width: 300px;
}
.header
{
background-color: gray;
font-weight: bold;
color: #fff;
}
.table th, .table td
{
border: 1px solid black;
padding: 5px;
}
</style>
<h2>IndexViewModel</h2>
// This is For EmpDetail in Grid
<fieldset>
<legend><b>uSER Details</b></legend>
<table border="1" cellpadding="10">
<tr>
<th>
@Html.DisplayNameFor(model =>model.id)
</th>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.Password)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.RegDateTime)
</th>
<th>
Action
</th>
</tr>
@* @foreach (var item in (IEnumerable<EMS1.Models.UsersViewModel>)ViewBag.Name) *@
@foreach (var item in Model.users)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.id)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Password)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.RegDateTime)
</td>
<td>
@Html.ActionLink("Edit", "Index", new { id = item.id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.id })
</td>
</tr>
}
</table>
</fieldset>
// This is For EmpDetail in webGrid
<fieldset>
<legend><b>uSER Details</b></legend>
<div id="content">
@grid.GetHtml(
tableStyle:"webgrid-table",
headerStyle:"webgrid-header",
footerStyle:"webgrid-footer",
alternatingRowStyle:"webgrid-alternating-row",
rowStyle:"webgrid-row-style",
columns:grid.Columns(
//here i will add column for serial no
grid.Column(columnName:"id",header:"id"),
grid.Column(columnName:"UserName", header:"Username"),
grid.Column(header:"Email", format:@<text><a href="mailto:@item.Email">@item.Email</a></text>)
))
</div>
</fieldset>
// This is for the Emp Entry Screen
<div class="form-horizontal">
@using (Html.BeginForm("Create","Home"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend> <b>Entry Screen</b></legend>
<div class="form-group">
@Html.LabelFor( model => model.UserName)
<div class="col-md-10">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
</div>
<div class="form-group">
<p>
<input type="submit" value="Create" name="Create"
style=@((ViewBag.Operation != null && Convert.ToInt32(ViewBag.Operation) > 0) ? "display:none" : "display:block") />
<input type="submit" value="Update" name="Update"
style=@((ViewBag.Operation != null && Convert.ToInt32(ViewBag.Operation) > 0) ? "display:block" : "display:none") />
</p>
</div>
</fieldset>
}
</div>
and here is controller
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EMS1.Models;
using System.Data;
namespace EMS1.Controllers
{
public class HomeController : Controller
{
ExecuteTsql ts = new ExecuteTsql();
public ActionResult PartialView()
{
ViewBag.Message = "Welcome to my demo!";
return View();
}
[HttpPost]
// [HttpParamAction]
[ValidateAntiForgeryToken]
public ActionResult Create(UsersViewModel objEmp)
{
if (ModelState.IsValid)
{
ts.IUD("insert into tbl_user (username,password,email,regdatetime) values( " + "'" + objEmp.UserName + "'," + "'" + objEmp.Password + "'," + "'" + objEmp.Email + "','" + DateTime.Now + "')");
//db.tblEmps.Add(objEmp);
//db.SaveChanges();
}
//return RedirectToAction("Index");
return RedirectToAction("IndexViewModel", "Home");
}
public List<TBL_USER> GetUsers()
{
System.Data.DataSet ds = new System.Data.DataSet();
ds = ts.SelectQueryDS("select * from tbl_user");
List<TBL_USER> user = new List<TBL_USER>();
int table = Convert.ToInt32(ds.Tables.Count);// count the number of table in dataset
for (int i = 0; i < table; i++)// set the table value in list one by one
{
foreach (DataRow dr in ds.Tables[i].Rows)
{
user.Add(new TBL_USER { id = Convert.ToInt32(dr["id"]), UserName = Convert.ToString(dr["UserName"]), Password = Convert.ToString(dr["Password"]), Email = Convert.ToString(dr["Email"]), RegDateTime = Convert.ToDateTime(dr["RegDateTime"]) });
}
}
// model. = cust;
//teachers.Add(new Teacher { TeacherId = 1, Code = "TT", Name = "Tejas Trivedi" });
//teachers.Add(new Teacher { TeacherId = 2, Code = "JT", Name = "Jignesh Trivedi" });
//teachers.Add(new Teacher { TeacherId = 3, Code = "RT", Name = "Rakesh Trivedi" });
return user;
}
public ActionResult IndexViewModel()
{
ViewBag.Message = "Welcome to my demo!";
EMS1.Models.UsersViewModel mymodel = new EMS1.Models.UsersViewModel();
List<TBL_USER> teachers = new List<TBL_USER>();
mymodel.users = GetUsers();
return View(mymodel);
}
public ActionResult IndexViewData()
{
ViewBag.Message = "Welcome to my demo!";
ViewData["users"] = GetUsers();
// ViewData["Students"] = GetStudents();
return View();
}
public ActionResult Index()
{
ViewBag.Message = "";
// FormsAuthentication.SignOut();
return View();
}
//public ActionResult Index(int? id)
//{
// //ViewBag.Operation = id;
// //ViewBag.Name = db.tblEmps.ToList();
// TBL_USER objEmp = new TBL_USER
// //tblEmp objEmp = db.tblEmps.Find(id);
// .. return View(objEmp);
//}
public ActionResult Inbox()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
can any one help what code code shouid I written for edit and delete ..
please do let me know when need to share other chunks of code
i m facing a problem in writting action methods for same