We provide IT Services

Register Now Click On

Read More Tutorials Click On

Post Page Advertisement [Top]


Model  Class:- Data Model Registration:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace mvc_code_first.Models
{
   [Table("tblregistration")] 
    public class Registration
    {
        [Key]
        public int Empid { get; set; }
        public string Fname { get; set; }
        public string Lname { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string Phone { get; set; }
        public Nullable<DateTime> create_date { get; set; }
        public Nullable<DateTime> Modified_date { get; set; }
    }
}


Step2:- Create DbContext class:-

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using AutoMapper;

namespace mvc_code_first.Models
{
    public class EmpDataContext : DbContext
    {
        public EmpDataContext()
               : base("name=Defaultconnection")
        {

        }
        public DbSet<Registration> registrations { get; set; }
        public DbSet<Header> header { get; set; }
    }
}

Step 3 View model:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace mvc_code_first.Models
{
    public class RegistrationViewModel
    {
        public int Empid { get; set; }
        public string Fname { get; set; }
        public string Lname { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string Phone { get; set; }
        public Nullable<DateTime> create_date { get; set; }
        public List<Registration> registraionRecordList { get; set; }
    }

}


.CS page Code Controller:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mvc_code_first.Models;
using AutoMapper;
using System.Data.Entity;
using System.Net;
using System.Net.Mail;

namespace mvc_code_first.Controllers
{
    public class HomeController : Controller
    {
        EmpDataContext db = new EmpDataContext();
        RegistrationViewModel regvm = new RegistrationViewModel();
        List<Registration> list = new List<Registration>();
        Registration reg = new Registration();

        [HttpGet]
        public ActionResult Index(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                int empid = Convert.ToInt32(id);
                reg = db.registrations.FirstOrDefault(x => x.Empid == empid);
                regvm.Empid = empid;
                regvm.Fname = reg.Fname;
                regvm.Lname = reg.Lname;
                regvm.Age = reg.Age;
                regvm.Address = reg.Address;
                regvm.Email = reg.Email;
                regvm.Password = reg.Password;
                regvm.Phone = reg.Phone;
                regvm.create_date = reg.create_date;
            }
            List<Registration> recordList = db.registrations.ToList();
            foreach (var item in recordList)
            {
                list.Add(item);
            }
            regvm.registraionRecordList = list;
            return View(regvm);
        }

        public ActionResult Delete(string id)
        {
            int emp = Convert.ToInt16(id);
            reg = db.registrations.Find(emp);
            db.registrations.Remove(reg);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult Insert(RegistrationViewModel data)
        {
            // mail start
            MailMessage mm = new MailMessage("rajatgupta3103@gmail.com","deveshagrawal1995@gmail.com");
            mm.Subject = "test";
            mm.Body = "my 1st mail program";
            mm.IsBodyHtml = false;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;

            NetworkCredential nc = new NetworkCredential("rajatgupta3103@gmail.com","loveyoumomdad");
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = nc;
            smtp.Send(mm);
            // mail end
            reg.Empid = data.Empid;
            reg.Fname = data.Fname;
            reg.Lname = data.Lname;
            reg.Age = data.Age;
            reg.Address = data.Address;
            reg.Email = data.Email;
            reg.Password = data.Password;
            reg.Phone = data.Phone;
            reg.create_date = reg.Empid == 0 ? DateTime.Now : data.create_date;
            if (reg.Empid == 0)
            {
                db.registrations.Add(reg);
            }
            else
            {
                reg.Modified_date = DateTime.Now;
                db.Entry(reg).State = EntityState.Modified;
            }
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult HeaderTitle ()
        {
            return View();
        }

        [HttpPost]
        public ActionResult HeaderTitle(Header her)
        {
            her.create_date = DateTime.Now;
            db.header.Add(her);
            db.SaveChanges();
            return View();
        }
    }
}


View Code:-


@model mvc_code_first.Models.RegistrationViewModel
@{
    ViewBag.Title = "User Registration";
}
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<style>
    @@media print {
        .hide-in-print {
            display: none;
        }
    }
</style>
<div class="row hide-in-print">
    <div class="col-md-12">
        <div class="title_left">
            <h3>Detail</h3>
        </div>
    </div>
</div>
<div class="row hide-in-print">
    @using (Html.BeginForm("Insert", "Home", FormMethod.Post))
    {
        @Html.HiddenFor(x => x.Empid)
        @Html.HiddenFor(x => x.create_date)
        <div class="form-group">
            @Html.LabelFor(x => x.Fname, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Fname, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Lname, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Lname, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(x => x.Age, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Age, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(x => x.Address, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Address, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(x => x.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Email, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Password, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(x => x.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(x => x.Phone, new { htmlAttributes = new { @class = "Form-control" } })
            </div>
        </div>

        <br />
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10" style="margin-top:10px">
                <input id="btnsave" type="submit" value="Save" class="btn btn-success" />  <input type="button" class="btn btn-success" value="Go to Admin" onclick="location.href='@Url.Action("HeaderTitle", "Home")'" />
            </div>
        </div>
    }

</div>
<br />
@{
    if (Model != null && Model.registraionRecordList.Count > 0)
    {
        @Html.Partial("_Getdata");
    }
    else
    {
        <div class="text-danger">No Records Found !!</div>
    }
    <center>
        <div>
            <button id="btnprint" class="fa fa-print hide-in-print" style="font-size:25px;color:black"></button>
        </div>
    </center>
}
<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>


Patial View Code:-

@model mvc_code_first.Models.RegistrationViewModel
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<table class="table">
    <tr class="btn-primary alert-danger">
        <th>First Name</th>
        <th>Last Name</th>
        <th>Age</th>
        <th>Address</th>
        <th>Email</th>
        <th>Password</th>
        <th>Phone</th>
        <th>Delete</th>
    </tr>


    @foreach (var item in Model.registraionRecordList)
    {
        <tr class="btn-info">
            <td>
                @Html.HiddenFor(x => item.Empid)
                @if (!string.IsNullOrEmpty(item.Fname))
                {
                    @Html.ActionLink(item.Fname, "Index", "Home", new { id = item.Empid },null);
                }
            </td>
            <td>
                @Html.DisplayFor(x => item.Lname)
            </td>
            <td>
                @Html.DisplayFor(x => item.Age)
            </td>
            <td>
                @Html.DisplayFor(x => item.Address)
            </td>
            <td>
                @Html.DisplayFor(x => item.Email)
            </td>
            <td>
                @Html.DisplayFor(x => item.Password)
            </td>
            <td>
                @Html.DisplayFor(x => item.Phone)
            </td>
            <td>
                @*<i id="btndlt" class="fa fa-trash-o" style="font-size:25px;color:red"onclick="location.href='@Url.Action("Delete", "Home", new { id = item.Empid })'"></i>*@
                @Html.ActionLink("Delete", "Delete", "Home", new { id = item.Empid }, null)
            </td>
        </tr>
    }
</table>
<link href="~/scripts/sweetalert.css" rel="stylesheet" />
<script src="~/scripts/sweetalert.min.js"></script>
<script src="~/scripts/sweetalert.js"></script>

<script>
 
</script>

<script src="~/scripts/jquery-1.10.2.min.js"></script>
<script>


</script>



2 comments:

Unknown said...

What is the difference between Sorted Dictionary and Ordered Dictionary.
Can multiple data sets be returned in Entity Framework? If yes, please explain how.
What is 2 way data binding in AngularJS.
What is Difference $timeout and $interval in angularJS
Which of the following is not the content of the assembly manifest
Which of the following is not true about .Net structure.
When remoting clients has to share the same data, which architecture should be used

Anonymous said...

what is synchronization in mvc-
Since your application is hosted on IIS, it's maintained by the application pool process. If you create additional module for your task, it will be running within context of the same process. You have to be sure this process is still working in the middle of the night, when application is not used, in order to perform the synchronization you want. You can use Quartz.NET to schedule your sync task.

Post a comment

| Designed by Rockprogrammer