Insert and Get value multi checkbox box list using MVC 5

Insert and Get value multi checkbox box list using MVC 5



[HTTPGET]

=========================

 [HttpGet]
        public ActionResult UserProfile()     // Show form data page load time
        {
            ProfileViewModel obj = new ProfileViewModel();
            string username = Convert.ToString(Session["Username"]);
            SignUp objsignup = db.signUp.Where(x => x.Username == username).FirstOrDefault();
 List<Hobbies> objSelectedHobbies = FillHobbies();
            if (!string.IsNullOrEmpty(objsignup.Hobbies))
            {
           

                string s = objsignup.Hobbies;
                string[] values = s.Split(',');

                //for (int i = 0; i < objSelectedHobbies.Count(); i++)
                //{
                //    objSelectedHobbies[i] == values[i]
                //}

                for (int i = 0; i < values.Length; i++)
                {
                    int selectedValue = Convert.ToInt32(values[i]);

                    for (int j = 0; j < objSelectedHobbies.Count; j++)
                    {
                        if (selectedValue == objSelectedHobbies[j].Value)
                        {
                            objSelectedHobbies[j].Selected = true;
                        }
                    }
                }


                ViewData["Hobbies"] = objSelectedHobbies;
            }
         

            obj.Name = objsignup.Name;
            obj.Username = objsignup.Username;
            obj.Password = objsignup.Password;
            obj.Gender = objsignup.Gender;
            obj.Email_Address = objsignup.Email_Address;
            obj.DateofBirth = objsignup.DateofBirth;
            obj.Address = objsignup.Address;
            obj.Mobile = objsignup.Mobile;
            obj.Profession = objsignup.Profession;
            obj.Country = objsignup.Country;
            obj.State = objsignup.State;
            obj.District = objsignup.District;
            obj.Image = objsignup.Image;
            obj.IsActive = objsignup.IsActive;
            obj.ID = objsignup.ID;
            obj.HobbiesList = objSelectedHobbies;

            //    obj.HobbiesList = new ProfileViewModel
            //    {
            //        HobbiesList = new[] {
            //    new Hobbies { Text = "Chess", Value = 1 },


            //}.ToList()
            //    };



            //obj
            Fill();
            //FillHobbies();

            return View(obj);
        }


=========================



[HTTPPOST]

=========================
 [HttpPost]
        public ActionResult UserProfile( ProfileViewModel model)
        {
            if (ModelState.IsValid)
            {
                string hobbies = string.Empty;
                var selectedHobbies = model.HobbiesList.Where(x => x.Selected == true).Select(x => x.Value).ToList();

                hobbies = string.Join(",", selectedHobbies);
                 
                string username = Convert.ToString(Session["Username"]);  // username by using session
                var Objsignup = db.signUp.Find(model.ID);     // find database by ID

                // var Objsignup = db.signUp.Where(x => x.Username == username).FirstOrDefault(); // find query base database Username find , isse bi find kr skte h

                Objsignup.Name = model.Name;
                Objsignup.Username = model.Username;
                Objsignup.Password = model.Password;
                Objsignup.Gender = model.Gender;
                Objsignup.Email_Address = model.Email_Address;
                Objsignup.DateofBirth = model.DateofBirth;
                Objsignup.Address = model.Address;
                Objsignup.Mobile = model.Mobile;
                Objsignup.Profession = model.Profession;
                Objsignup.Country = model.Country;
                Objsignup.State = model.State;
                Objsignup.District = model.District;
                Objsignup.Image = model.Image;
                Objsignup.IsActive = model.IsActive;
                Objsignup.Hobbies = hobbies;

                db.Entry(Objsignup).State = EntityState.Modified;//
                db.SaveChanges();
                Fill();
                FillHobbies();
                return View(model);
            }
            Fill();                   // exception stop dropdownlist fill
            FillHobbies();
            return View();
        }
========================


FIll method
===========================

private void Fill()
        {
            var list = new List<string>() { "India", "China", "Amrica", "Japan" };
            ViewBag.list = list;

            //List<SelectListItem> State = new List<SelectListItem>();
            //State.Add(new SelectListItem { Text = "Rajasthan", Value = "0" });
            //State.Add(new SelectListItem { Text = "Gujraat", Value = "1" });
            //State.Add(new SelectListItem { Text = "Delhi", Value = "2" });
            //State.Add(new SelectListItem { Text = "Haryana", Value = "3" });
            //ViewData["TaskStateList"] = State;
        }

==========================


Fill Hobbies
=========================

 private List<Hobbies> FillHobbies()
        {
            List<Hobbies> hobbies = new List<Hobbies>();
            hobbies.Add(new Hobbies { Text = "Chess", Value = 1 });
            hobbies.Add(new Hobbies { Text = "Gujraat", Value = 2 });
            hobbies.Add(new Hobbies { Text = "Reading Book", Value = 3 });
            hobbies.Add(new Hobbies { Text = "Running", Value = 4 });
            ViewData["Hobbies"] = hobbies;

            return hobbies;
        }

=============================



VIew

==========================



@model MvcPart1.Areas.Admin.Models.ProfileViewModel
@{
    ViewBag.Title = "UserProfile";
    Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}

 @using (Html.BeginForm("UserProfile", "Home", FormMethod.Post, new { @class = "form-horizontal" }))
            {
                @Html.AntiForgeryToken()
                @Html.HiddenFor(m => m.ID)


 <div class="form-group">
                                @Html.LabelFor(model => model.Hobbies, htmlAttributes: new { @class = "col-md-3 col-xs-12 control-label" })
                                @*<label class="col-md-3 col-xs-12 control-label">Checkbox</label>*@
                                <div class="col-md-6 col-xs-12">
                                    <label class="check">

                                        @for ( int i = 0;   i < Model.HobbiesList.Count(); i++)
                                         {
                                            @Html.CheckBoxFor(x => x.HobbiesList[i].Selected)                                       
                                            @Html.LabelFor(x => x.Hobbies, Model.HobbiesList[i].Text)
                                            @Html.HiddenFor(x => x.HobbiesList[i].Value)
                                            @Html.HiddenFor(x => x.HobbiesList[i].Text)
                                        }

                                        @*@Html.EditorFor(model => model.Hobbies, new { htmlAttributes = new { @class = "icheckbox", @checked = "checked" } })*@
                                        @*@Html.ValidationMessageFor(model => model.Hobbies, "", new { @class = "text-danger" })*@
                                        @*<input type="checkbox" class="icheckbox" checked="checked" /> Checkbox title*@
                                    </label>
                                    <span class="help-block">Checkbox sample, easy to use</span>
                                </div>
                            </div>
 }





Profileviewmodel.cs  and Hobbies.cs

=================================


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

namespace MvcPart1.Areas.Admin.Models
{
    public class ProfileViewModel
    {
        public int ID { get; set; }

        [Required(ErrorMessage = "Please Enter Name")]
        [Display(Name = "Name")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Please Enter Your Username")]
        [Display(Name = "Username")]
        public string Username { get; set; }

        //[Required(ErrorMessage = "Please Enter Password")]
        [Display(Name = "Password")]
        public string Password { get; set; }

       // [Required(ErrorMessage = "Please Select Gender")]
        [Display(Name = "Gender")]
        public string Gender { get; set; }

        //[Required(ErrorMessage = "Please Select Date of Birth")]
        [Display(Name = "Date of Birth")]
        public DateTime DateofBirth { get; set; }

        //[Required(ErrorMessage = "Please Enter Email Address")]
        [Display(Name = "Email")]
        public string Email_Address { get; set; }

        //[Required(ErrorMessage = "Please Enter Address")]
        [Display(Name = "Address")]
        public string Address { get; set; }

        //[Required(ErrorMessage = "Please Enter Mobile No.")]
        [Display(Name = "Mobile")]
        public int Mobile { get; set; }

        //[Required(ErrorMessage = "Please Select Country")]
        [Display(Name = "Country")]
        public string Country { get; set; }


       // [Required(ErrorMessage = "Please Select State")]
        [Display(Name = "State")]
        public string State { get; set; }

        //[Required(ErrorMessage = "Please Select District")]
        [Display(Name = "District")]
        public string District { get; set; }

        //[Required(ErrorMessage = "Please Enter Profession")]
        [Display(Name = "Profession")]
        public string Profession { get; set; }

       // [Required(ErrorMessage = "Please Chosse Hobbies")]
        [Display(Name = "Hobbies")]
        public bool Hobbies { get; set; }

        // [Required(ErrorMessage = "Please Your Profile Image")]
        [Display(Name = "Image")]
        public string Image { get; set; }

        public bool IsActive { get; set; }
        public bool IsDelete { get; set; }
        public List<Hobbies> HobbiesList { get; set; }
    }

    public class Hobbies
    {
        public string Text { get; set; }
        public int Value { get; set; }
        public bool Selected { get; set; }
    }
}

==================================

Comments

Popular posts from this blog

Tree view in winforms using c#

how to Replace null value with 0 Using C#

how to fetch all HTML Table Records and modify all records accordingly using jquery and Javascript