i have following 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";
}
<script src="@Url.Content("~/Scripts/jquery-1.6.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<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("Edit", "Edit", 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>),
grid.Column(header:"DElete", format: @<text>@Html.ActionLink("Delete", "Delete", new { id = item.Id } )</text>),
grid.Column(header:"Edit", format: @<text>@Html.ActionLink("Edit", "Edit", new { id = item.Id } )</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>
it working fine for delete and insertion and display .. i need to work it in edit mode too .. when i click on edit button in grild it should display records in fields f suername and password ..but it is not working ..
[HttpPost]
// [HttpParamAction]
[ValidateAntiForgeryToken]
public ActionResult Create(UsersViewModel objEmp)
{
if (ModelState.IsValid)
{
ds = ts.SelectQueryDS("select * from tbl_user where username = " + "'" + objEmp.UserName + "'");
if (ds.Tables[0].Rows.Count > 0)
{
}
else
{
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 ActionResult Edit(int id=0)
{
DataSet ds = new DataSet();
ds = ts.SelectQueryDS("select * from tbl_user where id =" + id);
List<TBL_USER> userprofile = 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)
{
userprofile.Add(new TBL_USER { id = Convert.ToInt32(dr["id"]), UserName = Convert.ToString(dr["UserName"]) });
}
}
// TBL_USER userprofile = db.UserProfiles.Find(id);
if (userprofile == null)
{
return HttpNotFound();
}
return View(userprofile);
// return RedirectToAction("IndexViewModel", new { id = 0 });
}