Tuesday, March 28, 2017

MVC Demo

-----main controller--------


 Elaunch_DemoEntities context = new Elaunch_DemoEntities();
        const int pageSize = 10;
        // GET: Student
        public ActionResult Index(int id = 0)
        {
            StudentMaster objstu = new StudentMaster();
            if (id == 0)
            {
                ViewBag.check = "Add";
                return View();
            }
            else
            {
                ViewBag.check = "Update";
                objstu = context.StudentMasters.Find(id);
            }
            return View(objstu);

        }
        [HttpGet]
        public ActionResult GetAllStud(int page = 1, int sortby = 1, bool isAsc = true, string search = null)
        {
            IEnumerable<StudentMaster> Stuobj = context.StudentMasters.Where(p => search == null || p.Name.Contains(search) || p.Gender.Contains(search) || p.hobbies.Contains(search) || p.BirthDate.ToString().Contains(search));
            #region Sorting
            //switch (sortby)
            //{
            //    case 1:
            //        Stuobj = isAsc ? Stuobj.OrderBy(p => p.Name) : Stuobj.OrderByDescending(p => p.Name);
            //        break;
            //    case 2:
            //        Stuobj = isAsc ? Stuobj.OrderBy(p => p.Gender) : Stuobj.OrderByDescending(p => p.Gender);
            //        break;
            //    case 3:
            //        Stuobj = isAsc ? Stuobj.OrderBy(p => p.hobbies) : Stuobj.OrderByDescending(p => p.hobbies);
            //        break;
            //    default:
            //        Stuobj = isAsc ? Stuobj.OrderBy(p => p.BirthDate) : Stuobj.OrderByDescending(p => p.BirthDate);
            //        break;


            //}
            #endregion
            Stuobj = Stuobj.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            ViewBag.CurrentPage = page;
            ViewBag.PageSize = pageSize;
            ViewBag.TotalPage = Math.Ceiling((double)context.StudentMasters.Count() / pageSize);
            ViewBag.Search = search;
            ViewBag.SortBy = sortby;
            ViewBag.IsAsc = isAsc;
            return View(Stuobj);
        }
        [HttpPost]
        public ActionResult createupdate(StudentMaster obj)
        {
            ModelState["StudentId"].Errors.Clear();
            if (ModelState.IsValid)
            {
                StudentMaster objstud = new StudentMaster();
                if (obj.StudentId > 0)
                {
                    objstud.Name = obj.Name;
                    objstud.BirthDate = obj.BirthDate;
                    objstud.Gender = obj.Gender;
                    objstud.hobbies = obj.hobbies;
                    objstud.StudentId = obj.StudentId;
                    context.Entry(obj).State = System.Data.Entity.EntityState.Modified;
                    context.SaveChanges();
                }
                else
                {
                    objstud.StudentId = obj.StudentId;
                    objstud.Name = obj.Name;
                    objstud.hobbies = obj.hobbies;
                    objstud.Gender = obj.Gender;
                    objstud.BirthDate = obj.BirthDate;
                    context.StudentMasters.Add(objstud);
                    context.SaveChanges();
                    Response.Write("Insert SuccssesFully");
                }
                return RedirectToAction("GetAllStud");
            }
            else
            {
                return View("Index");

            }
        }
        public ActionResult Delete(int id)
        {
            var obj = context.StudentMasters.Find(id);
            context.StudentMasters.Remove(obj);
            context.SaveChanges();
            return RedirectToAction("GetAllStud");
        }




---------View Index---------------

@model studentRegistrationMVC.Models.StudentMaster

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@*@using (Html.BeginForm("createupdate", "Student", FormMethod.Post))*@
@using (Html.BeginForm("createupdate", "Student", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="row">
        <br />
        <h4>@ViewBag.check Student Registration</h4>
        <div class="row">
            @Html.HiddenFor(x=>x.StudentId)
            <div class="form-group">
                @Html.LabelFor(x => x.Name)
                @Html.TextBoxFor(x => x.Name, new { @class = "form-control" })
                @Html.ValidationMessageFor(x => x.Name, "", new { @class = "text-danger" })
            </div>
            <div class="form-group">
                @Html.LabelFor(x => x.BirthDate)
                @Html.TextBoxFor(x => x.BirthDate, new { @class = "form-control" })
                @Html.ValidationMessageFor(x => x.BirthDate, "", new { @class = "text-danger" })
            </div>
            <div class="form-group">
                @Html.LabelFor(x => x.hobbies)
                @Html.DropDownListFor(x => x.hobbies, new List<SelectListItem> { new SelectListItem { Value = "Reading", Text = "Reading" }, new SelectListItem { Value = "dancing", Text = "dancing" } }, new { @class = "form-control" })
                @Html.ValidationMessageFor(x => x.hobbies, "", new { @class = "text-danger" })
            </div>
            <div class="form-group">
                @Html.LabelFor(x => x.Gender)
                @Html.RadioButtonFor(x => x.Gender, "Male", true) Male
                @Html.RadioButtonFor(x => x.Gender, "Female", true) Female
                @Html.ValidationMessageFor(x => x.Gender, "", new { @class = "text-danger" })
            </div>
          <div class="form-group">
              <input type="submit" value="Save" class="btn btn-success" />
          </div>
        </div>
    </div>

                    }



--------view GetAllstud-----------
@model IEnumerable<studentRegistrationMVC.Models.StudentMaster>
@{
    ViewBag.Title = "GetAllStud";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h4>Student List</h4>
<div class="row">
    <div class="col-sm-12">
        <div class="product-search">
            <form action="@Url.Action("GetAllStud", "Student")" method="get">
                Search<input id="search" name="search" type="text" value="@ViewBag.Search" />
                @*<input type="submit" value="Search" />*@
            </form>
        </div>
        <div class="pull-right">
            <a href="@Url.Action("Index", "student")"><input type="button" name="Add Student" value="Add Student" /></a>
        </div>
        <table class="table table-hover">
            <thead>
                <tr>

                    <th>Name</th>
                    <th>Birth Date</th>
                    <th>Gender</th>
                    <th>Hobbies</th>
                    <th>Action</th>
                </tr>
            </thead>

            @foreach (var stu in Model)
            {
                <tbody>
                    <tr>
                        <td>@Html.Label(stu.Name)</td>


                        <td>@Html.Label(stu.BirthDate.Value.ToString("dd/MM/yyyy"))</td>


                        <td>@Html.Label(stu.Gender)</td>


                        <td>@Html.Label(stu.hobbies)</td>
                        <td>
                            @Html.ActionLink("Edit", "Index", new { id = stu.StudentId })
                            @Html.ActionLink("Delete", "Delete", new { id = stu.StudentId })
                        </td>

                    </tr>
                </tbody>


            }

        </table>
        <div class="pagination">
            Page:
            @for (int p = 1; p <= ViewBag.TotalPage; p++)
            {
                <a class="@(p==ViewBag.CurrentPage?"Current":"")" href="@Url.Action("GetAllStud", "Student", new {Page=p })">@p</a>
            }
        </div>

    </div>
</div>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
    $(document).ready(function () {
        $("#search").change(function (data) {
            var searchId = $("#search").val();
            window.location.href = "/Student/GetAllStud?search=" + searchId;
            //$.getJSON("/Student/GetAllStud?", {
            //    search: searchId
            //});
            //alert(search);
        })
    })

   
</script>

No comments:

Post a Comment

Demo for Repository Pattern in ASP.Net

----------------------------------------------------------- ----------------------------------------------------------- Repository Projec...