Asp.Net Mvc复选框列表

时间:2013-08-05 04:43:20

标签: asp.net asp.net-mvc razor checkbox html-helper

我正在MVC中实现复选框列表,虽然我成功获得了预期的结果,但我的方法存在疑问:

public class AdditionalServicesModel
{
    public IList<SelectListItem> AdditionalServices { get; set; }
}

=============================================== ==========================

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        AdditionalServicesModel objAdditionalServicesModel = new AdditionalServicesModel();

        List<SelectListItem> services = new List<SelectListItem>();
        services.Add(new SelectListItem { Text = "service-1", Value = "1", Selected=false });
        services.Add(new SelectListItem { Text = "service-2", Value = "2", Selected=false });
        services.Add(new SelectListItem { Text = "service-3", Value = "3", Selected=false });
        objAdditionalServicesModel.AdditionalServices = services;

        return View(objAdditionalServicesModel);
    }

    [HttpPost]
    public ActionResult Index(AdditionalServicesModel result)
    {
        return RedirectToAction("Thanks", "Home");
    }

    public ActionResult Thanks()
    {
        return View();
    }
}

=============================================== ==========================

@model checkboxes.Models.AdditionalServicesModel
@{
    ViewBag.Title = "Index";
    Layout = null;
}


@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    for (int i = 0; i < Model.AdditionalServices.Count; i++)
    {        
        <ul>
            <li>
                @Html.CheckBoxFor(m=>Model.AdditionalServices[i].Selected, new { id = "Chk-" + i})
                @Html.Label(Model.AdditionalServices[i].Text, new { @for = "Chk-" + i })
                @Html.HiddenFor(m => Model.AdditionalServices[i].Text)
                @Html.HiddenFor(m=> Model.AdditionalServices[i].Value)
            </li>
        </ul>
    }
    <input type="submit" value="POST to Controller" />
}

1)对于一个复选框,我应该创建2个额外的隐藏字段。 有更好的方法吗?我觉得只需要发送复选框值+名称就可以做到这一点是错误的。用javascript收集它的值并通过Json发送它会更容易,但是我不会有一个不显眼的验证... < / p>

2)我发送所有复选框,而我只需要发送选中的复选框。 有没有办法用表格发布?

1 个答案:

答案 0 :(得分:4)

创建自定义'服务复选框项'类(可重用),并在AdditionalServicesModel中创建列表或枚举它们作为属性。 此外,创建模型的构造函数可能更好,因此您不必在控制器内分配模型属性。

public class ServicesItem
{
     public bool Selected { get; set; }
     public string Value { get; set; }
     public string Text { get; set; }
}

public class AdditionalServicesModel
{
    public AdditionalServicesModel(IList<ServicesItem> items){
         this.AdditionalServices = items;
    }

    public IList<ServicesItem> AdditionalServices { get; set; }
}

为附加服务创建自定义编辑器模板,以便在视图中轻松引用(您不必为文本添加隐藏,因为只有值和选定属性将默认绑定回模型:

  @Html.CheckBoxFor(m => Selected)
  @Html.LabelFor(m => Text)
  @Html.HiddenFor(m => Value)

然后将编辑器模板弹出到您的视图中(让MVC.net发挥其魔力 ​​- 查看标记以查看它的作用):

@Html.EditorFor(m => m.AdditionalServices)

然后检查控制器中的自动绑定值:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<SelectListItem> services = new List<SelectListItem>();
            services.Add(new SelectListItem { Text = "service-1", Value = "1", Selected=false });
            services.Add(new SelectListItem { Text = "service-2", Value = "2", Selected=false });
            services.Add(new SelectListItem { Text = "service-3", Value = "3", Selected=false });

            return View(new AdditionalServicesModel(services));
        }

    [HttpPost]
    public ActionResult Index(AdditionalServicesModel result)
    {
         var selectedServicesList = result.AdditionalServices.Where(s => s.Selected);
         return RedirectToAction("Thanks", "Home");
    }
}