gpt4 book ai didi

Asp.Net Mvc 复选框列表

转载 作者:行者123 更新时间:2023-12-02 10:43:18 24 4
gpt4 key购买 nike

我正在 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) 我发送所有复选框,而我只需要发送选定的框。有没有办法用表单发布来做到这一点?

最佳答案

创建自定义“服务复选框项”类(可重用),并将它们作为附加服务模型中的属性进行列表或枚举。另外,创建模型的构造函数可能是一个更好的主意,这样您就不必在 Controller 内分配模型属性。

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

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

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

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

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

然后检查 Controller 中自动绑定(bind)的值:

    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");
}
}

关于Asp.Net Mvc 复选框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18050706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com