Having already used @Html.CheckBoxFor
within a View
, I plan to use with the former Tag Helper
in conjunction to ViewBag
within the same View
to circumvent few errors (e.g. variable definition):
MODEL
public class test
{
public int ItemID { get; set; }
public string ItemName { get; set; }
public bool IsAvailable { get; set; }
}
CONTROLLER
List<test> ItemList = new List<test>();
ItemList.Add(new test {ItemID=1, ItemName="apple", IsAvailable = false});
ItemList.Add(new test {ItemID=2, ItemName="mango", IsAvailable = false});
ItemList.Add(new test {ItemID=3, ItemName="stuck", IsAvailable = false});
ItemList.Add(new test {ItemID=4, ItemName="blocked", IsAvailable = false});
ItemList.Add(new test {ItemID=5, ItemName="help:(", IsAvailable = false});
ViewBag.ItemList = ItemList;
VIEW
<table>
@foreach (var item in ViewBag.ItemList)
{
<tr><td>
<input type= "checkbox" id = "Check_@item.ItemID" checked="@item.IsAvailable"
onclick="CheckOnlyOneCheckBox(this);"/>
<label for="Check_@item.ItemID">@item.ItemName</label>
</tr>
}
</table>
2 main issues have been encountered:
(a) Value of the selected box
could not retrieved from a submit button
using
foreach (var item in ViewBag.ItemList)
{
if (item.IsCheck)
{
var zz = item.ItemName;
}
}
(b) format of the displayed ItemName
using <input type= "checkbox">
look slightly different (e.g. bold font) than the ones obtained from a previous checkbox list
using @Html.CheckBoxFor
.
Thus any assistance in using @Html.CheckBoxFor
with ViewBag
would highly be appreciated.
EDIT
@Md Farid Uddin Kiron
The main issue lies on the fact that a checkbox
list has already been defined (in the same same View
) via:
List<test> chk = new List<test>();
chk.Add(new test() {ReferalTypeId=1, ReferalTypeName = "box1",
IsReferalCheck=false});
chk.Add(new test() {ReferalTypeId=2, ReferalTypeName =
"box2", IsReferalCheck=false});
chk.Add(new test() {ReferalTypeId=3, ReferalTypeName =
"Other", IsReferalCheck=false});
CtrList chklist = new CtrList(); // Ctrl being a public class defined
// in MODEL
chklist.reflist = chk;
return View(chklist);
Empirically I've ended up to a situation, where I could not define within public IActionResult Index()
another checkbox
list returning something else than chklist
since the variable (e.g. list
) won't be defined while looping it from View
.
As a result I had to use, as a way around: ViewBag
, enabling me to pass variable from Model
to View
without any problem. Without binding
it, this effective way around has been raising another issue and the solution of last resort I am investigating consists of:
(a) looping
through the checkbox
;
(b) identifying the selected value via ViewBag
;
(c) then finding a way to pass the selected variable from View
to Controller
. However, I had to concede that this latter approach looks sub-optimal.
Best