开发者

Determining number of selected check boxes in view

开发者 https://www.devze.com 2023-03-23 09:39 出处:网络
I have a .NET MVC application with a view that generates HTML containing multiple tables (this varies according to the user\'s input selections from the previous view).Each table can have a varying nu

I have a .NET MVC application with a view that generates HTML containing multiple tables (this varies according to the user's input selections from the previous view). Each table can have a varying number of check boxes (controlled by the user's selections from the previous view) that the user can use to makes selections that will be sent to a final report. The check boxes are being written to the .aspx file via HTML helpers generated in a 'for' loop:

    <%: Html.CheckBox("optionSelect" + checkCounter)%>

checkCounter has been added so that each check box has a different name that follows an ascending numerical pattern. The numbering does not get reset if the next check box is part of a new table (i.e., the 3 check boxes in the first table are "optionSelect1" through "optionSelect3", the 3 check boxes in the second table are "optionSelect4" through "optionSelect6").

What would be a good way to determine which check boxes in this view have been selected so that that information may be used by the controller class upon submission of the user's selections? At the very least, is there a way to tell the controller class the total number of check boxes that have been generated for the view? I'm new 开发者_C百科to the .NET MVC framework, so if others can point out other options I have missed, I would appreciate it.


You're kinda using checkboxes wrong here; instead, you should simply define a specific name for all of the checkboxes, and then the values of those will be posted to the server comma delimited under that name:

<input type="checkbox" id="1" name="Shared" value="1" />
<input type="checkbox" id="2" name="Shared" value="2" />
<input type="checkbox" id="3" name="Shared" value="3" />
<input type="checkbox" id="4" name="Shared" value="4" />
<input type="checkbox" id="5" name="Shared" value="5" />

In this case, when you check 1,3 and 5, when you post to the server you would get something like this:

public ActionResult Sample(string Shared)
{
      // Shared is "1,3,5"
}

You seem to be overcomplicating things instead of simplifying down to basic HTML forms =D

0

精彩评论

暂无评论...
验证码 换一张
取 消