23 Apr 2019

How to Bind dropdown in mvc code first approch


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

1 comment:
Write comments

Popular Posts

Popular Posts

Translate

Total Pageviews