在的CheckBoxList MVC3.0 [英] CheckboxList in MVC3.0

查看:101
本文介绍了在的CheckBoxList MVC3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能创建asp.net MVC一个的CheckBoxList,然后用的CheckBoxList来处理该事件。

How can I create a checkboxList in asp.net MVC and then to handle the event with the checkboxList

推荐答案

您可以有一个视图模型:

You could have a view model:

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

一个视图(〜/查看/主页/ Index.cshtml

@model IEnumerable<AppName.Models.MyViewModel>
@{
    ViewBag.Title = "Home Page";
}
@using (Html.BeginForm())
{
    @Html.EditorForModel()
    <input type="submit" value="OK" />
}

和相应的编辑模板(〜/查看/主页/ EditorTemplates / MyViewModel.cshtml

@model AppName.Models.MyViewModel
@Html.HiddenFor(x => x.Id)           
@Html.CheckBoxFor(x => x.IsChecked)

现在,当您提交表单,你会得到价值和它是否被选中与否每个值列表。

Now when you submit the form you would get a list of values and for each value whether it is checked or not.

这篇关于在的CheckBoxList MVC3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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