问题
I am having a form in a view page that looks as below:
<form runat="server" id="dcrsubmit">
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>test1</asp:ListItem>
<asp:ListItem>test2</asp:ListItem>
<asp:ListItem>test3</asp:ListItem>
<asp:ListItem>test4</asp:ListItem>
<asp:ListItem>test5</asp:ListItem>
<asp:ListItem>test6</asp:ListItem>
</asp:CheckBoxList>
....other controls
</form>
Now when the form is posted, I am trying to retrieve the values submitted in the controller as below:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult testmethod(FormCollection formValues)
{
string s = formvalues.get("CheckBoxList1");
.
. /* other code */
.
}
The string value shows null when I submit the form by checking some checkboxes. Is this the way to retrieve the values or am I doing something wrong? And I cannot use html control because all other controls on the form are server controls and I am not sure if I can only make this control a html control. And I am not sure how can I bind the values to it?
回答1:
MVCContrib supply some excellent extensions to provide many controls and they also have a CheckBoxList.
To get started with MVCContrib, read this
You'll need to use a strongly typed view and include this import statement in the view:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.HomeModel>" %>
<%@ Import Namespace="MvcContrib.FluentHtml" %>
This example works. Here is the code on the view.
<%= this.CheckBoxList("UserType").Options(Model.UserTypeOptions) %>
Here is the controller.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Submit(HomeModel viewModel)
{
if (viewModel.SelectedUserType == UserTypeEnum.Normal)
{
// do something
}
return View("Index", viewModel);
}
Here is the model.
public enum UserTypeEnum
{
Administrator = 0,
SuperUser,
Supervisor,
Normal,
}
public class HomeModel
{
[Required]
public string Name { get; set; }
public List<SelectListItem> UserTypeOptions { get; set; }
public string UserType { get; set; }
public UserTypeEnum SelectedUserType
{
get
{
return (UserTypeEnum) Enum.Parse(typeof (UserTypeEnum), UserType);
}
}
public HomeModel()
{
UserTypeOptions = new List<SelectListItem>
{
new SelectListItem{Text = "Administrator", Value = ((int)UserTypeEnum.Administrator).ToString()},
new SelectListItem{Text = "SuperUser", Value = ((int)UserTypeEnum.SuperUser).ToString()},
new SelectListItem{Text = "Supervisor", Value = ((int)UserTypeEnum.Supervisor).ToString()},
new SelectListItem{Text = "Normal", Value = ((int)UserTypeEnum.Normal).ToString()},
};
}
}
回答2:
I think what you need is how gather selected values from CheckBoxList that user selected and here is my solution for that:
1- Download Jquery.json.js and add it to your view as refrence:
<script src="../../Scripts/jquery.json.js" type="text/javascript"></script>
2- I've added a ".cssMyClass" to all checkboxlist items so I grab the values by their css class:
<script type="text/javascript" >
$(document).ready(function () {
$("#btnSubmit").click(sendValues);
});
function populateValues()
{
var data = new Array();
$('.myCssClas').each(function () {
if ($(this).attr('checked')) {
var x = $(this).attr("value");
data.push(x);
}
});
return data;
}
function sendValues() {
var data = populateValues();
$.ajax({
type: 'POST',
url: '@Url.Content("~/Home/Save")',
data: $.json.encode(data),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function () { alert("1"); }
});
}
</script>
3- As you can see I've added all selected values to an Array and I've passed it to "Save" action of "Home" controller by ajax 4- in Controller you can receive the values by adding an array as argument:
[HttpPost]
public ActionResult Save(int[] val)
{
I've searched too much but apparently this is the only solution. Please let me know if you find a better solution for it.
来源:https://stackoverflow.com/questions/2701333/how-to-retrieve-checkboxlist-values-in-the-controller-in-asp-net-mvc