Asp.Net Mvc复选框列表

我在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发送它会容易得多,但是我不会有一个不显眼的验证...

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


创建一个自定义的“服务复选框项目”类(可重用),并将它们的列表或可枚举为您的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");
    }
}
链接地址: http://www.djcxy.com/p/73259.html

上一篇: Asp.Net Mvc Checkbox list

下一篇: Can we use RTTI to find functions/procedures by name and run them?