how to bind table data in checkboxlist in mvc 4?

Nov 21 2013 7:49 AM
view:
@model IBLAZING.Models.PROJECT

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <div class="content">
        <div class="fieldset">
    <fieldset>
        <legend>PROJECT</legend>
        <link href="~/Content/layout.css" rel="stylesheet" />
       
        @*<div class="editor-label">
            @Html.LabelFor(model => model.PROJ_ID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PROJ_ID)
            @Html.ValidationMessageFor(model => model.PROJ_ID)
        </div>*@

        <div class="editor-label">
            @Html.LabelFor(model => model.PROJ_TITLE)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PROJ_TITLE)
            @Html.ValidationMessageFor(model => model.PROJ_TITLE)
        </div>
        <div class="clear"></div>
        @*<div class="editor-label">
            @Html.LabelFor(model => model.TECH_ID, "TECHNOLOGY")
        </div>
        <div class="editor-field">
            @Html.DropDownList("TECH_ID", "--- Select Technology-Name---")
            @Html.ValidationMessageFor(model => model.TECH_ID)
        </div>*@
         <div class="clear"></div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CLIENT_ID, "CLIENT")
        </div>
        <div class="editor-field" >
            @Html.DropDownList("CLIENT_ID", "---Please Select Client-Name---" )
            @Html.ValidationMessageFor(model => model.CLIENT_ID)
        </div>
         <div class="clear"></div>

        <div class="editor-label">
            @Html.LabelFor(model => model.PROJ_DETAIL)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PROJ_DETAIL)
            @Html.ValidationMessageFor(model => model.PROJ_DETAIL)
        </div>
         <div class="clear"></div>
        <div class="editor-label">
            @Html.LabelFor(model => model.EMP_ID, "EMPLOYEE")
                    
        </div>
        <div class="editor-field" aria-multiselectable="True">
       
     <script src="~/Scripts/jquery-1.7.1.min.js"></script>

 

<script>

    $(document).ready(function () {

      

 

        $('.chkclass').click(function () {

          

            var getchkid = $(this).attr('id');

            var isChecked = $('#' + getchkid).is(':checked');

 

            if ($('#' + getchkid).is(':checked') == true) {

                $('#td' + $(this).val()).css("color", "white");

                $('#td' + $(this).val()).css("background-color", "blue");

            }

            else {

                $('#td' + $(this).val()).css("color", "black");

                $('#td' + $(this).val()).css("background-color", "white");

            }

        });

 

    });

 

</script>
            <div id="divstudentlist" class="elem" style="height: 110px; overflow: auto;border:solid; width:150px;">

@foreach (var names in @Model.EmployeeNames)

{

    var checkBoxId = "chk" + names.Value;

    var tdId = "td" + names.Text;

                    <table width="100%">

                        <tr >

                            <td width="20px">

                                <input type="checkbox" id="@checkBoxId" class="chkclass" value="@names.Value" />

                            </td>

                            <td id="@tdId"  width="100px">

                                @names.Text


                            </td>

                        </tr>

                    </table>

 

}

    </div>
           @Html.DropDownListFor(m => m.EMPLOYEE,(IEnumerable<SelectListItem>)ViewBag.EMP_ID,
        "--Please select--")
          
           @* @Html.CheckBox("EMP_ID")*@

         
           
            @Html.DropDownList("EMP_ID","--- Select Employee-Name---")
          
            @Html.ValidationMessageFor(model => model.EMP_ID)
        </div>
       
       
         <div class="clear"></div>
        <div class="editor-label">
            @Html.LabelFor(model => model.AWARD_TIME)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AWARD_TIME)
            @Html.ValidationMessageFor(model => model.AWARD_TIME)
        </div>
         <div class="clear"></div>
        <div class="editor-label">
            @Html.LabelFor(model => model.START_TIME)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.START_TIME)
            @Html.ValidationMessageFor(model => model.START_TIME)
        </div>
         <div class="clear"></div>
        <div class="editor-label">
            @Html.LabelFor(model => model.COST)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.COST)
            @Html.ValidationMessageFor(model => model.COST)
        </div>
         <div class="clear"></div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
        </div>
        </div>
}
    
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

my model:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace IBLAZING.Models
{
    using System;
    using System.Collections.Generic;
    using System.Web.Mvc;
   
    public partial class PROJECT
    {
  
        public IList<SelectListItem> EmployeeNames { get; set; }
   
   
        public PROJECT()
        {
            this.PRIOJ_TO_EMPLOYEE = new HashSet<PRIOJ_TO_EMPLOYEE>();
            this.PROJ_DOC = new HashSet<PROJ_DOC>();
            this.PROJ_TO_TECH = new HashSet<PROJ_TO_TECH>();
            this.PROJECT_DISCUSSION = new HashSet<PROJECT_DISCUSSION>();
        }
   
        public int PROJ_ID { get; set; }
        public string PROJ_TITLE { get; set; }
        public int CLIENT_ID { get; set; }
        public string PROJ_DETAIL { get; set; }
        public int EMP_ID { get; set; }
        public System.DateTime AWARD_TIME { get; set; }
        public System.DateTime START_TIME { get; set; }
        public decimal COST { get; set; }
   
        public virtual CLIENT CLIENT { get; set; }
        public virtual EMPLOYEE EMPLOYEE { get; set; }
        public virtual ICollection<PRIOJ_TO_EMPLOYEE> PRIOJ_TO_EMPLOYEE { get; set; }
        public virtual ICollection<PROJ_DOC> PROJ_DOC { get; set; }
        public virtual ICollection<PROJ_TO_TECH> PROJ_TO_TECH { get; set; }
        public virtual ICollection<PROJECT_DISCUSSION> PROJECT_DISCUSSION { get; set; }
    }
}

my controller:

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

namespace IBLAZING.Controllers
{
    public class ProjectsController : Controller
    {
        private IblazingEntities db = new IblazingEntities();

        //
        // GET: /Projects/

        public ActionResult Index()
        {
            //var topics=(from p in db.PROJECTS join pt in db.PRIOJ_TO_EMPLOYEE on p.PROJ_ID equals pt.PROJ_ID
            //            join pt in db.PRIOJ_TO_EMPLOYEE on p.EMP_ID =pt.EMP_ID
            //            select new
            //            {
            //                PROJ_ID=p.PROJ_ID ,
            //                PROJ_TITLE =p.PROJ_TITLE ,
            //                EMP_ID =p.EMP_ID
            //            }).ToList();
            var projects = db.PROJECTS.Include(p => p.CLIENT).Include(p => p.EMPLOYEE);
            return View(projects.ToList());
            //return View (topics);
        }

        //
        // GET: /Projects/Details/5

        public ActionResult Details(int id = 0)
        {
            PROJECT project = db.PROJECTS.Find(id);
            if (project == null)
            {
                return HttpNotFound();
            }
            return View(project);
        }

        //
        // GET: /Projects/Create

        public ActionResult Create()
        {

            PROJECT project = new PROJECT();
            List<SelectListItem> names = new List<SelectListItem>();

            //ViewBag.EMP_ID = db.EMPLOYEEs.Select(X => new SelectListItem { Text = X.EMP_NAME, Value = X.EMP_ID.ToString() });
           

            names.Add(new SelectListItem{ Text = "EMP_NAME", Value = "EMP_ID" });

            project.EmployeeNames = names;

            ViewBag.CLIENT_ID = new SelectList(db.CLIENTs, "CLIENT_ID", "CLIENT_NAME");
           
           ViewBag.EMP_ID = db.EMPLOYEEs.Select(X => new SelectListItem { Text = X.EMP_NAME, Value = X.EMP_ID.ToString() });
         
            ViewBag.EMP_ID = new SelectList(db.EMPLOYEEs, "EMP_ID", "EMP_NAME");
            ViewBag.TECH_ID = new SelectList(db.TECHNOLOGies, "TECH_ID", "TECH_NAME");
            return View(project);
        }

        //
        // POST: /Projects/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(PROJECT project)
        {
            if (ModelState.IsValid)
            {
                db.PROJECTS.Add(project);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CLIENT_ID = new SelectList(db.CLIENTs, "CLIENT_ID", "CLIENT_NAME", project.CLIENT_ID);
            ViewBag.EMP_ID = new SelectList(db.EMPLOYEEs, "EMP_ID", "EMP_NAME", project.EMP_ID);
           // ViewBag.TECH_ID = new SelectList(db.TECHNOLOGies, "TECH_ID", "TECH_NAME", project.TECH_ID);
            return View(project);
        }

        //
        // GET: /Projects/Edit/5

        public ActionResult Edit(int id = 0)
        {
            PROJECT project = db.PROJECTS.Find(id);
            if (project == null)
            {
                return HttpNotFound();
            }
            ViewBag.CLIENT_ID = new SelectList(db.CLIENTs, "CLIENT_ID", "CLIENT_NAME", project.CLIENT_ID);
            ViewBag.EMP_ID = new SelectList(db.EMPLOYEEs, "EMP_ID", "EMP_NAME", project.EMP_ID);
            //ViewBag.TECH_ID = new SelectList(db.TECHNOLOGies, "TECH_ID", "TECH_NAME", project.TECH_ID);
            return View(project);
        }

        //
        // POST: /Projects/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(PROJECT project)
        {
            if (ModelState.IsValid)
            {
                db.Entry(project).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CLIENT_ID = new SelectList(db.CLIENTs, "CLIENT_ID", "CLIENT_NAME", project.CLIENT_ID);
            ViewBag.EMP_ID = new SelectList(db.EMPLOYEEs, "EMP_ID", "EMP_NAME", project.EMP_ID);
           // ViewBag.TECH_ID = new SelectList(db.TECHNOLOGies, "TECH_ID", "TECH_NAME", project.TECH_ID);
            return View(project);
        }

        //
        // GET: /Projects/Delete/5

        public ActionResult Delete(int id = 0)
        {
            PROJECT project = db.PROJECTS.Find(id);
            if (project == null)
            {
                return HttpNotFound();
            }
            return View(project);
        }

        //
        // POST: /Projects/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            PROJECT project = db.PROJECTS.Find(id);
            db.PROJECTS.Remove(project);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
in this i want to display employee name in checkboxlist in project page

Answers (1)