Thursday, August 16, 2018

Checkbox binding


@using MyMVC.Models
@model CountryViewModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm("Save", "Home", FormMethod.Post, new { id = "form_Home", enctype = "multipart/form-data" }))
{
     <div class="form-group">
        <label for="count">Country</label>
      
         @Html.DropDownList("ddlcountry",new SelectList(Model.CountryList,"CountryId","CountryName"),"Select",new { @id = "ddltopicgroups", @style = "width:550px;"})
    </div>
    
    <ul>
        @for (int i = 0; i < Model.CountryList.Count; i++)
        {
            <li>
             <input type="checkbox" id="chk_@Model.CountryList[i].CountryId" name="CountryList[@i].CountryId" @(Model.CountryList[i].CheckedStatus?"Checked":"") value="@(Model.CountryList[i].CountryId)" class="chkCountry" />
                
                 <label for="lbl_@Model.CountryList[i].CountryId">@Model.CountryList[i].CountryName</label>
                
                <input type="hidden" id="hdn_@Model.CountryList[i].CountryId" name="CountryList[@i].CheckedStatus" value="@Model.CountryList[i].CheckedStatus.ToString().ToLower()" />

                <input type="hidden" id="hdn_@Model.CountryList[i].CountryName" name="CountryList[@i].CountryName" value="@Model.CountryList[i].CountryName" />


             @*   @Html.CheckBoxFor(m => m.CountryList[i].CheckedStatus, new { @class = "chkCountry" })           
                
                @Html.DisplayFor(m => m.CountryList[i].CountryName)
                @Html.HiddenFor(m => m.CountryList[i].CountryId, new {@class="hdnCountryId" })
                @Html.HiddenFor(m => m.CountryList[i].CountryName) *@               
            </li>
        }
    </ul>
    <input type="button" id="btnSave" name="save" value="Save" />
    
    <br />
     @ViewBag.Country
} 
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
    
    $(function () {
        
        $('#btnSave').click(function () {
            var postdata = JSON.stringify(GetFormDataAsJson("#form_Home"));


            console.log(postdata);
            $.ajax({
                type: "POST",
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                url: '/Home/Save',
                data: postdata,
                async: false,
                success: function (data) {
                    alert("Saved Sucessfully", "Done");
                },
                error: function (data) {

                    alert(data.responseText + "Fail", "Failed");
                }

            });

        });
    });

    function GetFormDataAsJson(frm) {
      
        var formObject = $(frm).serializeArray();
        var data = {};
        $.each(formObject, function (index, ele) {
            var groupIds = [];
            var groupobjects = $.map(formObject, function (obj, index) {
                if (obj.name == ele.name) {
                    groupIds.push(obj.value);
                    return obj;
                }
            });
            if (groupobjects.length > 1 && groupIds.length > 1) {
                data[ele.name] = groupIds;
            }
            else {
                data[ele.name] = ele.value;
            }
        });
        return data;
    }
  
  
</script>

===================================================
 public ActionResult Index()
        {
            CountryViewModel countryViewModel = new CountryViewModel();
            countryViewModel.CountryList = BindCountry();

            return View(countryViewModel);
        }
 public List<Country> BindCountry()
        {
            List<Country> lstCountry = new List<Country>()
            {
                new Country{CountryId=1,CountryName="India",CheckedStatus=true},
                new Country{CountryId=2,CountryName="UK",CheckedStatus=false},
                new Country{CountryId=3,CountryName="USA",CheckedStatus=false},
                new Country{CountryId=4,CountryName="AUS",CheckedStatus=true},
                new Country{CountryId=5,CountryName="PAK",CheckedStatus=false},
            };
            return lstCountry;
        }
===================================
 public JsonResult Save(CountryViewModel _objcountrymodel)
        {
            var model = _objcountrymodel;
            _objcountrymodel.CountryList = _objcountrymodel.CountryList.Where(m => m.CheckedStatus == true).Select(k => k).ToList();
            string sb = string.Join(",", _objcountrymodel.CountryList.Select(k => k.CountryName).ToList());

            ViewBag.Country = sb.ToString();

            return Json(true, JsonRequestBehavior.AllowGet);
        }

No comments:

Post a Comment

javascript Filter/index off

 var family = [{"name":"Jack",  "age": 26},               {"name":"Jill",  "age"...