英文:
maintain the selected options and checkboxes after submit
问题
我有一个表单内的复选框和选择选项。
<label>
<input type="checkbox" name="checkedBoxes" value="Reseller" checked />
<span>Reseller</span>
</label>
<label>
<input type="checkbox" name="checkedBoxes" value="SI" checked />
<span>SI</span>
</label> //selects
...
复选框显示为已选中。
<input id="checkedBoxes" type="submit" value="Display" class="btn" />
</form>
这是我的控制器的样子:
public IActionResult combinedForm(string[] checkedBoxes, string SelectOption, string SelectOptionComp, string SelectOptionSer, string SelectOptionSoft, string SelectOptionApp)
{ //viewModel=...
var deneme = viewModel.Where(x => x.Companies.company_type.Contains(checkedBoxes[0]));
}
如何在单击提交按钮时保持复选框的状态,最好是选择的选择选项也保持在视图中?
<details>
<summary>英文:</summary>
I have checkboxes and select options inside a form.
<label>
<input type="checkbox" name="checkedBoxes" value="Reseller"checked />
<span>Reseller</span>
</label>
<label>
<input type="checkbox" name="checkedBoxes" value="SI" checked/>
<span>SI</span>
</label> //selects
...
Checkboxes are displayed as checked.
<input id="checkedBoxes" type="submit" value="Display" class="btn" />
</form>
This is what my controller looks like:
public IActionResult combinedForm(string[] checkedBoxes, string SelectOption, string SelectOptionComp, string SelectOptionSer, string SelectOptionSoft, string SelectOptionApp)
{ //viewModel=...
var deneme = viewModel.Where(x =>x.Companies.company_type.Contains(checkedBoxes[0]));
}
How can I keep the state of checkboxes, preferably with the selected select options on view when I clicked the submit button?
</details>
# 答案1
**得分**: 0
以下是翻译好的部分:
```html
Used viewbags for both
@if (((string[])ViewBag.checkedBoxes).Contains("Component provider"))
{
<label>
<input type="checkbox" name="checkedBoxes" value="Component provider" checked />
<span>Component provider </span>
</label>
}
else if (!((string[])ViewBag.checkedBoxes).Contains("Component provider"))
{
<label>
<input type="checkbox" name="checkedBoxes" value="Component provider" />
<span>Component provider </span>
</label>
}
----
<select style="width:100px" id="SelectOption" name="SelectOption" value="a">
<option>@Html.Label("-", (string)ViewBag.Name, new {@class = "mylabel" })</option>
</select>
请注意,我已经将 HTML 代码中的 HTML 实体编码进行了还原,以便更容易阅读。
英文:
Used viewbags for both
@if (((string[])ViewBag.checkedBoxes).Contains("Component provider"))
{
<label>
<input type="checkbox" name="checkedBoxes" value="Component provider" checked />
<span>Component provider&nbsp;</span>
</label>
}
else if (!((string[])ViewBag.checkedBoxes).Contains("Component provider"))
{
<label>
<input type="checkbox" name="checkedBoxes" value="Component provider" />
<span>Component provider&nbsp;</span>
</label>
}
<select style="width:100px" id="SelectOption" name="SelectOption" value="a">
<option>@Html.Label("-",(string)ViewBag.Name,new {@class = "mylabel" })</option>
</select>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论