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 });
        }

but it is not working

What exactly is not working?

when i click on edit ..it should display selected record in edit mode in textbox fields

thanks all for reply ..I have Done CURD operation with single view except editing ... let me share code

@model EMS1.Models.UsersViewModel

@{
    ViewBag.Title = "PersonDetais";
}

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

<table id="_list">
    <tr>
        <td colspan="3">
            <h3>User List</h3>
        </td>
    </tr>
    <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>  
                        @Html.DisplayNameFor(model => model.Active)  
                    </th>  
        <th></th>
    </tr>

    @foreach (var item in (IEnumerable<EMS1.Models.TBL_USER>)ViewData["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.CheckBoxFor(modelItem => item.Active)  
                        </td>  
            <td>
                @Html.ActionLink("Edit", "IndexViewModel", new { id = item.id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.id })
            </td>
        </tr>
    }

</table>

    <div id="_create">
        <div>
            <h3>Person @((ViewData["Operation"] != null && Convert.ToInt32(ViewData["Operation"]) > 0) ? "Edit" : "Create")</h3>
        </div>
        <fieldset>
            <legend>Person</legend>



             <div class="editor-label">
                @Html.LabelFor(model => model.UserName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.UserName)
                @Html.ValidationMessageFor(model => model.UserName)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Password)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Email)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Email)
                @Html.ValidationMessageFor(model => model.Email)
            </div>



              <div class="editor-label">
                @Html.LabelFor(model => model.Active)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Active)
                @Html.ValidationMessageFor(model => model.Active)
            </div>
            <p>
                <input type="submit" value="Create" name="Create"  
                    style=@((ViewData["Operation"] != null && Convert.ToInt32(ViewData["Operation"]) > 0) ? "display:none" : "display:block") />
                <input type="submit" value="Update" name="Update"
                       style=@((ViewData["Operation"] != null && Convert.ToInt32(ViewData["Operation"]) > 0) ? "display:block" : "display:none") />
            </p>

        </fieldset>
    </div>
}
   public ActionResult IndexViewModel(int? id)
        {
            EMS1.Models.UsersViewModel mymodel = new EMS1.Models.UsersViewModel();
            // EMS1.Models.TBL_USER mymodel = new EMS1.Models.TBL_USER();
            ViewData["Operation"] = id;
            if (Request.IsAuthenticated)
            {
                ViewBag.Message = "Welcome to my demo!";

                List<TBL_USER> teachers = new List<TBL_USER>();
                mymodel.users = GetUsers("select   * from tbl_user");
                ViewData["users"] = mymodel.users;
                if (id != null)
                {
                    mymodel.users = GetUsers("select   * from tbl_user where id = " + id);

                }

                //Person db.Persons.Find(id);

                //if (id == null)
                //{
                //    mymodel.users = GetUsers("select   * from tbl_user");
                //    ViewData["users"] = mymodel.users;
                //}
                //else
                //{
                //    mymodel.users = GetUsers("select   * from tbl_user where id = " + id);
                //    ViewData["users"] = mymodel.users;
                //}



                //ViewData["users"] = mymodel.users;
                return View(mymodel);
            }
            else
            {
                return View("index");

            }


        }
  public List<TBL_USER> GetUsers(string str)
        {
            System.Data.DataSet ds = new System.Data.DataSet();

            ds = ts.SelectQueryDS(str);
            List<TBL_USER> user = new List<TBL_USER>();
        //   List<UsersViewModel> user = new List<UsersViewModel>();
            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"]), Active =  Convert.ToBoolean(dr["Active"]) });
                 //   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"]), Active = Convert.ToBoolean(dr["Active"]) });
                }
            }

          //  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;
        }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EMS1.Models
{
    public class UsersViewModel
    {
        //public List<TBL_USER> Customers { get; set; }

        //public List<TBL_USER> users { get; set; }
        public int id { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
        public Nullable<System.DateTime> RegDateTime { get; set; }

        public bool Active { get; set; }
        public IEnumerable<TBL_USER> users { get; set; }

    }
}

i have shared all related code just before ..i have problem when i click on edit button in grid it hsould popualte related data from database in textboxes .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.