为了账号安全,请及时绑定邮箱和手机立即绑定

MVC3.0中的CheckboxList

MVC3.0中的CheckboxList

POPMUISE 2019-10-26 13:13:32
如何在asp.net MVC中创建一个checkboxList,然后使用checkboxList处理事件
查看完整描述

2 回答

?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

您可能有一个视图模型:


public class MyViewModel

{

    public int Id { get; set; }

    public bool IsChecked { get; set; }

}

控制器:


public class HomeController : Controller

{

    public ActionResult Index()

    {

        var model = new[] 

        {

            new MyViewModel { Id = 1, IsChecked = false },

            new MyViewModel { Id = 2, IsChecked = true },

            new MyViewModel { Id = 3, IsChecked = false },

        };

        return View(model);

    }


    [HttpPost]

    public ActionResult Index(IEnumerable<MyViewModel> model)

    {

        // TODO: Handle the user selection here

        ...

    }

}

视图(~/Views/Home/Index.cshtml):


@model IEnumerable<AppName.Models.MyViewModel>

@{

    ViewBag.Title = "Home Page";

}

@using (Html.BeginForm())

{

    @Html.EditorForModel()

    <input type="submit" value="OK" />

}

以及相应的编辑器模板(~/Views/Home/EditorTemplates/MyViewModel.cshtml):


@model AppName.Models.MyViewModel

@Html.HiddenFor(x => x.Id)           

@Html.CheckBoxFor(x => x.IsChecked)

现在,当您提交表单时,您将获得一个值列表,并且将为每个值选择是否选中。


查看完整回答
反对 回复 2019-10-26
  • 2 回答
  • 0 关注
  • 702 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信