views:

460

answers:

2

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?

A: 

Don't quite understand what your controller means. But as long as you enabled view state, you can get the value in code behind. Why don't you use the server control object directly, like cbPrintReicpt.Cheked. Also it is ok to make control a html control. You can also do it dynamically in your code. Just search it in google you will get enough results.

+1  A: 

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()},
                       };
    }
}
Mac