如何在Asp.net MVC中的控制器中检索复选框列表值 [英] How to retrieve checkboxlist values in the controller in asp.net mvc

查看:111
本文介绍了如何在Asp.net MVC中的控制器中检索复选框列表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查看页面中有一个表单,如下所示:

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 */
   .
}

当我通过选中某些复选框提交表单时,字符串值显示为空.这是检索值的方式还是我做错了什么?而且我不能使用html控件,因为表单上的所有其他控件都是服务器控件,而且我不确定是否只能将此控件设为html控件.而且我不确定如何将值绑定到它?

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?

推荐答案

MVCContrib提供了一些出色的扩展以提供许多控件,并且它们还具有CheckBoxList.

MVCContrib supply some excellent extensions to provide many controls and they also have a CheckBoxList.

要开始使用MVCContrib,请阅读

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) %>

这是控制者.

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Submit(HomeModel viewModel)
    {
        if (viewModel.SelectedUserType == UserTypeEnum.Normal)
        {
            // do something
        }
        return View("Index", viewModel);
    }

这是模型.

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

这篇关于如何在Asp.net MVC中的控制器中检索复选框列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆