埃拉德
会员
- 已加入
- 2020年2月15日
- 留言内容
- 16
- 编程经验
- 1-3
你好!
我有两个问题1:如何创建多个复选框,并要求用户至少选择一个复选框?
2.如何在有此要求的情况下使用CheckBoxFor循环在“视图”中创建?在这样的示例中,我在Internet上找不到任何内容,而只是循环运行并创建但没有测试字段的复选框的标准输入框。我正在寻找一些不需要ajax或javascript的简单解决方案,但实际上仅c#目录
我有两个问题1:如何创建多个复选框,并要求用户至少选择一个复选框?
2.如何在有此要求的情况下使用CheckBoxFor循环在“视图”中创建?在这样的示例中,我在Internet上找不到任何内容,而只是循环运行并创建但没有测试字段的复选框的标准输入框。我正在寻找一些不需要ajax或javascript的简单解决方案,但实际上仅c#目录
带有数据注释的模型:
namespace AllStore.Models
{
public class Store
{
int Id { set; get; }
[Required(ErrorMessage = "Required field!")]
[Display(Name = "Name")]
string Name { set; get; }
[Required(ErrorMessage = "Must choose at least one day!")]
[Display(Name = "All days in week")]
public IList<SelectListItem> DaysWork { get; set; }
public Store()
{
DaysWork = new List<SelectListItem>()
{
new SelectListItem {Text = "Sunday", Value = "1"},
new SelectListItem {Text = "Monday", Value = "2"},
new SelectListItem {Text = "Tuesday", Value = "3"},
new SelectListItem {Text = "Wednesday", Value = "4"},
new SelectListItem {Text = "Thursday", Value = "5"},
new SelectListItem {Text = "Friday", Value = "6"},
new SelectListItem {Text = "Saturday", Value = "7"},
};
}
}
}
}
and Controller is:
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AllStore.Controllers
{
public class 首页Controller : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult AddWorker()
{
AllStore.Models.Store store = new AllStore.Models.Store();
return View(store);
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
我不知道如何在foreach中设置一个复选框:
@model AllStore.Models.Store
@{
ViewBag.Title = "AddWorker";
}
<h2>AddWorker</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Store</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}