We provide IT Services

Register Now Click On

Read More Tutorials Click On

Post Page Advertisement [Top]



Controller Code:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Testmvc_12aprl.Models;
using System.Data.Entity;

namespace Testmvc_12aprl.Controllers
{
    public class HomeController : Controller
    {
        private _DbContext _db = new _DbContext();
        List<Registration> list = new List<Registration>();
        Registration regv = new Registration();
        Registrationviewmodel regvm = new Registrationviewmodel();
        List<Registrationviewmodel> tt = new List<Registrationviewmodel>();
        [HttpGet]
        public ActionResult Index(string id)
        {
            if(!string.IsNullOrEmpty(id))
            {
                int empid = Convert.ToInt16(id);
                regv = _db.registration.FirstOrDefault(x => x.Empid == empid);
                regvm.Empid = empid;
                regvm.Name = regv.Name;
                regvm.Age = regv.Age;
                regvm.Address = regv.Address;
                regvm.Phone = regv.Phone;
                regvm.Country_name = regv.Country_name;
                regvm.Email = regv.Email;
                regvm.Password = regv.Password;
            }
            ViewBag.countries = _db.country.ToList();
            List<Country> recorcountry = ViewBag.countries;
            regvm.registrationlist = _db.registration.ToList();
            List<Registration> reglist = _db.registration.ToList();
            //----------------------------------------
            var query = (from a in reglist
                         join b in recorcountry
                         on a.Country_name equals b.Cid
                         select new
                         {
                             a.Empid,
                             a.Name,
                             a.Age,
                             a.Address,
                             a.Phone,
                             a.Country_name,
                             a.Email,
                             a.Password,
                             b.Cname
                         }).ToList();


            foreach (var item in query)
            {
                Registrationviewmodel r = new Registrationviewmodel();
                r.Empid = item.Empid;
                r.Name = item.Name;
                r.Age = item.Age;
                r.Address = item.Address;
                r.Phone = item.Phone;
                r.Country_name = item.Country_name;
                r.Email = item.Email;
                r.Password = item.Password;
                r.Cname = item.Cname;
                tt.Add(r);
            }
            regvm.registrationviewmodellist = tt;
            return View(regvm);
        }
        public ActionResult Delete(string id)
        {
            int empid = Convert.ToInt16(id);
            regv = _db.registration.Find(empid);
            _db.registration.Remove(regv);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Insert(Registration data)
        {
            regv.Empid = data.Empid;
            regv.Name = data.Name;
            regv.Age = data.Age;
            regv.Address = data.Address;
            regv.Phone = data.Phone;
            regv.Country_name = data.Country_name;
            regv.Email = data.Email;
            regv.Password = data.Password;
            regv.Createdate = DateTime.Now;
            if (regv.Empid == 0)
            {
                _db.registration.Add(regv);
            }
            else
            {
                _db.Entry(regv).State = EntityState.Modified;         
             }
                _db.SaveChanges();
                return RedirectToAction("Index");
        }
    }
}

View Code:-

@model Testmvc_12aprl.Models.Registrationviewmodel
@{
    ViewBag.Title = "Index";
}
<br />
@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>Phone:</td>
            <td>@Html.TextBoxFor(x=>x.Phone)</td>
        </tr>

        <tr>
            <td>Country:</td>
            @*<td>@Html.DropDownList("Countries", new SelectList(ViewBag.countries as System.Collections.IEnumerable, "Cid", "Cname"), "--select--", new { id = "dd_country" })</td>*@
            <td>@Html.DropDownListFor(x=>x.Country_name,new SelectList(ViewBag.countries as System.Collections.IEnumerable,"Cid","Cname"),"--select--",new { id = "dd_country" })</td>
        </tr>
        <tr>
            <td>Email:</td>
            <td>@Html.TextBoxFor(x=>x.Email)</td>
        </tr>

        <tr>
            <td>Password:</td>
            <td>@Html.TextBoxFor(x=>x.Password)</td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" id="btnsave" value="Save" /></td>
        </tr>
    </table>
}
    @if (Model != null)
    {
        @Html.Partial("_Index");
    }
    else
    {
        <div class="text-danger">Record is not Fount !!</div>
    }
<script src="~/scripts/jquery-1.10.2.min.js"></script>
<script>
    $(document).ready(function () {
        if ($("#Empid").val() != 0)
        {
            $("#btnsave").val("Update")
        }
    });
</script>

Partial View:-

@model Testmvc_12aprl.Models.Registrationviewmodel
<table class="table">
    <tr class="btn-primary alert-danger">
        <th>Name</th>
        <th>Age</th>
        <th>Address</th>
        <th>Phone</th>
        <th>Country</th>
        <th>Email</th>
        <th>Password</th>
        <th>Delete</th>
        <th>Edit</th>
    </tr>
    @foreach (var item in Model.registrationviewmodellist)
    {
        //var countryname = "";
        //foreach(var country in ViewBag.countries)
        //{
        //    if (country.Cid == item.Country_name)
        //    {
        //        countryname = country.Cname;
        //        break;
        //    }
        //}
        <tr class="btn-info">
            <td>
                @Html.DisplayFor(x => item.Name)
            </td>
            <td>
                @Html.DisplayFor(x => item.Age)
            </td>
            <td>
                @Html.DisplayFor(x => item.Address)
            </td>
            <td>
                @Html.DisplayFor(x => item.Phone)
            </td>
            <td>
                @Html.DisplayFor(x => item.Cname)
            </td>
            <td>
                @Html.DisplayFor(x => item.Email)
            </td>
            <td>
                @Html.DisplayFor(x => item.Password)
            </td>
            <td>
                @Html.ActionLink("Delete", "Delete", "Home", new { id = item.Empid },null)
            </td>
            <td>
                @Html.ActionLink("Edit", "Index", "Home", new { id = item.Empid }, null)
            </td>
        </tr>
    }
</table>
Registrationviewmodel:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Testmvc_12aprl.Models
{
    public class Registrationviewmodel
    {
        public int Empid { get; set; }
        public string Name { get; set; }
        public DateTime Age { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
        public int Country_name { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string Cname { get; set; }
        public string Cid { get; set; }
        public DateTime? Createdate { get; set; }
        public DateTime? Modifieddate { get; set; }
        public List<Registration> registrationlist { get; set; }
        public List<Registrationviewmodel> registrationviewmodellist { get; set; }
        public List<Country> countrylist { get; set; }
    }

}



Registration.cs:-



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

namespace Testmvc_12aprl.Models
{
    [Table("tableregistration")]
    public class Registration
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Empid { get; set; }
        public string Name { get; set; }
        public DateTime Age { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
        public  int Country_name { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public DateTime? Createdate { get; set; }
        public DateTime? Modifieddate { get; set; }
    }
}



Dbcontext:-


public class _DbContext : DbContext
    {
        public _DbContext()
            : base("Defaultconnection")
        {
        }
        public DbSet<Registration> registration { get; set; }
        public DbSet<Country> country { get; set; }
    }

////////////////////////////////////////////////////////////////////////////////////////////////
public partial class changepassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["Username"] == null && Session["UserID"] == null)
                {
                    Response.Redirect("register.aspx");
                }
            }
        }
        protected void btnChangePassword_serverclick(object sender, EventArgs args)
        {
            try
            {
                DataTable dtTable = new DataTable();
                using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["sunshinetea"].ToString()))
                {
                    if (con != null)
                        con.Open();
                    using (SqlCommand cmd = new SqlCommand("UpdatePassword_sp", con))
                    {
                        DataSet ds = new DataSet();
                        cmd.CommandTimeout = 400;
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Userid", Convert.ToInt32(Session["UserID"]));
                        cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
                        cmd.ExecuteNonQuery();
                            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "clentscript", "alert('Password Updated Successfully.'); parent.location.href='default.aspx'", true);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

    }
///////////////////////////////////////////////////////////////////////////////////////////////

7 comments:

Anonymous said...

https://w3layouts.com/organic-store-an-agriculture-category-bootstrap-responsive-web-template/


https://demo.w3layouts.com/demos_new/template_demo/12-03-2019/organic_store-demo_Free/323914130/web/index.html#contact

Digveshwar Agrawal said...

hi rock

Anonymous said...

Best Tutorial Of Rock Programmer.......

Anonymous said...

https://stackoverflow.com/questions/24126819/jquery-sum-of-multiple-input-fields-if-same-class-in-one-input

Anonymous said...

http://spin.js.org/

Anonymous said...

What is Lazy Loding in c#
https://stackoverflow.com/questions/6847721/when-should-i-use-lazyt

Anonymous said...

jQuery DataTables and ASP.NET MVC Integration - Part I

https://www.codeproject.com/Questions/1200391/How-to-disable-the-secho-from-datatables

Post a Comment

| Designed by Rockprogrammer