We provide IT Services

Register Now Click On

Read More Tutorials Click On

Post Page Advertisement [Top]


How to add Controller:-


Step3:-


Step4:-How to add repository folder the create the the class in folder.


Step:6-



Controller code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using okhlainterview.Models;
using System.Data.Entity;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;

namespace okhlainterview.Controllers
{
    public class HomeController : Controller
    {
        Empcontext _db = new Empcontext();
        Registration regv = new Registration();
        List<Registration> list = new List<Registration>();
        registrationviewdata regvem = new registrationviewdata();

        public IList<Registration> GetRegistrationList()
        {
            var registrionList = (_db.registration).ToList();
            return registrionList;
        }

        [HttpGet]
        public ActionResult Index(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                int empid = Convert.ToInt16(id);
                regv = _db.registration.FirstOrDefault(x => x.Empid == empid);
                regvem.Empid = empid;
                regvem.Name = regv.Name;
                regvem.Age = regv.Age;
                regvem.Address = regv.Address;
            }

            IList<Registration> regvemlist = GetRegistrationList();
            foreach (var item in regvemlist)
            {
                list.Add(item);
                regvem.recordlist = list;
            }
            return View(regvem);
        }
        public ActionResult Delete(string id)
        {
            try
            {
                int empid = Convert.ToInt16(id);
                regv = _db.registration.Find(empid);
                _db.registration.Remove(regv);
                _db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw;
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Insert(registrationviewdata data)
        {
            regv.Empid = data.Empid;
            regv.Name = data.Name;
            regv.Age = data.Age;
            regv.Address = data.Address;
            if (regv.Empid == 0)
            {
                _db.registration.Add(regv);
            }
            else
            {
                _db.Entry(regv).State = EntityState.Modified;
            }
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Exporttoexcle()
        {
            List<Registration> regvemlist = _db.registration.ToList();
            foreach (var item in regvemlist)
            {
                list.Add(item);
                regvem.recordlist = list;
            }

            var gv = new GridView();
            gv.DataSource = this.GetRegistrationList();
            gv.DataBind();

            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=DemoExcel.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter objStringWriter = new StringWriter();
            HtmlTextWriter objHtmlTextWriter = new HtmlTextWriter(objStringWriter);
            gv.RenderControl(objHtmlTextWriter);
            Response.Output.Write(objStringWriter.ToString());
            Response.Flush();
            Response.End();
            return View("Index");
        }
    }

}

view code:

@model okhlainterview.Models.registrationviewdata
@{
    ViewBag.Title = "Index";
}

<style>
    @@media print {
        .hide-in-print {
            display: none;
        }
    }
</style>
<div class="row hide-in-print">
    @using (Html.BeginForm("Insert", "Home", FormMethod.Post))
    {
        @Html.HiddenFor(x => x.Empid);
        <table>
            <tr>
                <td>Name:</td>
                <td>@Html.TextBoxFor(x => x.Name)</td>
            </tr>

            <tr>
                <td>Age:</td>
                <td>@Html.TextBoxFor(x => x.Age)</td>
            </tr>

            <tr>
                <td>Address:</td>
                <td>@Html.TextBoxFor(x => x.Address)</td>
            </tr>

            <tr>
                <td><input type="submit" value="Save" id="btnsave" /></td>
            </tr>
        </table>
    }
</div>

    <div>
        @if (Model != null && Model.recordlist.Count > 0)
        {
            @Html.Partial("_Index");
        }
        else
        {
            <div class="text-danger"> Record does not fount !!</div>
        }
    </div>
    <div>
        <center>
            <div>
                <button id="btnprint" value="Printa">Print</button>
            </div>
        </center>
    </div>
<a href="@Url.Action("Exporttoexcle","Home")">Exportinexcle</a>

<script src="~/scripts/jquery-1.10.2.min.js"></script>
<script>
    $(document).ready(function () {
        if ($("#Empid").val() != 0) {
            $("#btnsave").val("Update");
        }
    });
    $("#btnprint").click(function () {
        window.print();
    });
</script>

Partial view code:-

@model okhlainterview.Models.registrationviewdata

<table class="table" id="tbl">
    <tr>
        <td>Name</td>
        <td>Age</td>
        <td>Address</td>
        <td>Delete</td>
    </tr>

    @foreach (var item in Model.recordlist)
    {
        <tr>
            <td>
                @if (!string.IsNullOrEmpty(item.Name))
                {
                    @Html.ActionLink(item.Name, "Index", "Home", new { id = item.Empid }, null);
                }
            </td>

            <td>
                @Html.DisplayFor(x => item.Age)
            </td>

            <td>
                @Html.DisplayFor(x => item.Address)
            </td>
            <td>
                @Html.ActionLink("Delete", "Delete", "Home", new { id = item.Empid },null)
            </td>

            <td>
                @Html.ActionLink("Edit", "Insert", "Home", new { id = item.Empid }, null)
            </td>
        </tr>
    }

</table>

Dbcontext class:-


   public class Empcontext:DbContext
    {
        public Empcontext()
                : base("Defaultconnection")
              {

              }
       public DbSet<Registration> registration { get; set; }
    }
}

2 comments:

My Tech India said...

All post is very good Rock Programmer

Anonymous said...

How to create rpt in mvc :-

https://www.c-sharpcorner.com/article/using-crystal-report-with-asp-net-mvc-5/

Post a Comment

| Designed by Rockprogrammer