I spent many days trying to pass the information from the View to the Controller
using @Html.CheckBox()
I have a textarea and a loop of checkboxes (we don't know how many). I can manage the textarea but not the checkboxes.
The View:
@using (Html.BeginForm("ProviderConfigurationTool", "Admin"))
{
<div>
<strong>Custom Rule </strong>
@Html.TextArea("CustomRule", new { rows = 12, columns = 30 })
<br />
</div>
foreach (var item in ViewBag.farmlist)
{
<table>
<tr>
<td style="border-bottom: solid 1px #EBEFF6">
<p>@item</p>
@Html.CheckBox("@item");
<!-- <input id="@item" name="@item" type="checkbox" onclick="addingFarms('@item')" class="farms" /> -->
</td>
</tr>
</table>
}
<input type="submit" value="Submit and close" onclick="refreshParent();self.close()" />
}
@{Html.EndForm();}
As you can I tried also with the normal html code using "type=checkbox"
The Controller:
[HttpPost]
public ActionResult Create(string CustomRule, FormCollection collection)
{
string results = collection["Blanks"]; // it does not work
newRule.Key = CustomRule; // it works
}
The other idea was using javascript and trying to create a List to pass to the controller like that
View:
input id="@item" name="@item" type="checkbox" onclick="getFarms('@item')" class="farms"
function getFarms() {
var inputs = document.querySelectorAll("input[type='checkbox']");
for (var i = 0; i < inputs.length; i++) {
inputs[i].checked = true;
//Here I am blocked because I don't know how to pass a new list to the controller
}
If somebody already got this problem or have an idea, please help me!