==================Model==================
public class Country
{
public int CountryId { get; set; }
public string CountryName { get; set; }
public bool CheckedStatus { get; set; }
}
public class CountryViewModel
{
public List
}
==============Controller======================
public ActionResult Index()
{
CountryViewModel countryViewModel = new CountryViewModel();
countryViewModel.CountryList = BindCountry();
return View(countryViewModel);
}
public ActionResult Save(CountryViewModel _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 View("Index", _objcountrymodel);
}
public List
{
List
{
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;
}
==============View==========================
@using MyMVC.Models
@model CountryViewModel
@{
ViewBag.Title = "Index";
}
Index
@using (Html.BeginForm("Save", "Home", FormMethod.Post, new { id = "form_Home", enctype = "multipart/form-data" }))
{
@for (int i = 0; i < Model.CountryList.Count; i++)
{
@*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
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" />*@
OR
@Html.CheckBoxFor(m => m.CountryList[i].CheckedStatus, new {@class="chkCountry",@id="chk"+Model.CountryList[i].CountryId})
@Html.DisplayFor(m => m.CountryList[i].CountryName)
@Html.HiddenFor(m => m.CountryList[i].CountryId, new {@class="hdnCountryId" })
@Html.HiddenFor(m => m.CountryList[i].CountryName)
}
/*< input type="submit" id="btnSave" name="save" value="Save" />*/
@ViewBag.Country
}
No comments:
Post a Comment