保持提交后选定的选项和复选框。

huangapple go评论63阅读模式
英文:

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.
 

     &lt;label&gt;
                &lt;input type=&quot;checkbox&quot; name=&quot;checkedBoxes&quot; value=&quot;Reseller&quot;checked /&gt;
                &lt;span&gt;Reseller&lt;/span&gt;
            &lt;/label&gt;
    
    
            &lt;label&gt;
                &lt;input type=&quot;checkbox&quot; name=&quot;checkedBoxes&quot; value=&quot;SI&quot; checked/&gt;
                &lt;span&gt;SI&lt;/span&gt;
            &lt;/label&gt; //selects
    ...
Checkboxes are displayed as checked.
    &lt;input id=&quot;checkedBoxes&quot; type=&quot;submit&quot; value=&quot;Display&quot; class=&quot;btn&quot; /&gt;
    &lt;/form&gt;
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 =&gt;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&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>

请注意,我已经将 HTML 代码中的 HTML 实体编码进行了还原,以便更容易阅读。

英文:

Used viewbags for both

 @if (((string[])ViewBag.checkedBoxes).Contains(&quot;Component provider&quot;))
{
&lt;label&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;checkedBoxes&quot; value=&quot;Component provider&quot; checked  /&gt;
&lt;span&gt;Component provider&amp;nbsp;&lt;/span&gt;
&lt;/label&gt;
}
else if (!((string[])ViewBag.checkedBoxes).Contains(&quot;Component provider&quot;))
{
&lt;label&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;checkedBoxes&quot; value=&quot;Component provider&quot; /&gt;
&lt;span&gt;Component provider&amp;nbsp;&lt;/span&gt;
&lt;/label&gt;
}

&lt;select style=&quot;width:100px&quot; id=&quot;SelectOption&quot; name=&quot;SelectOption&quot; value=&quot;a&quot;&gt;
&lt;option&gt;@Html.Label(&quot;-&quot;,(string)ViewBag.Name,new {@class = &quot;mylabel&quot; })&lt;/option&gt;

</select>

huangapple
  • 本文由 发表于 2023年1月9日 17:48:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定