asp.net-mvc – MVC 4,复选框列表和我

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – MVC 4,复选框列表和我前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
全部早上

我可以看到这已经在其他地方讨论过,但是想知道如果有什么改变或者事情在MVC 4中比较简单,像我这样的单纯形?

脚本

我有以下,编辑,型号:

public class CorporateDetails
{

    public Guid? Id { get; set; }

    [Key]
    public int CorporateDetailId { get; set; }

    public int? EmsId { get; set; }
    public string EmsName { get; set; }

    public virtual EmsType EmsType { get; set; }
}

public class EmsType
{
    [Key]
    public int? EmsId { get; set; }
    public string EmsName { get; set; }

    public virtual ICollection<EmsType> EmsTypes { get; set; }
}

使用以下标准创建视图:

<fieldset>
    <legend>CorporateDetails</legend>



    <div class="editor-label">
        @Html.LabelFor(model => model.EmsId,"EmsType")
    </div>
    <div class="editor-field">
        @Html.DropDownList("EmsId",String.Empty)
        @Html.ValidationMessageFor(model => model.EmsId)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.EmsName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.EmsName)
        @Html.ValidationMessageFor(model => model.EmsName)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

这给我开箱即用的一个美丽的下拉列表la Scott Gu’s blog

现在我真正的问题是这个 – 如何有效地将这个下拉框转换成什么将有效地成为一个多选,复选框列表?

再次,道歉过去,但我正在测试水,看是否有任何更新发生.

请注意,第一个MVC项目轻轻地走了,我又感觉很厚了:'(

解决方法

好的,我已经把它排序了 – hurray!从评论中可以看出,有一些问题出现,但请在下面找到完整的解决方案:D

模型

public class CorporateDetails
    {

        public Guid? Id { get; set; }

        [Key]
        public int CorporateDetailId { get; set; }

        public int[] EmsId { get; set; }

        }

    public class EmsType
    {
        [Key]
        public int EmsId { get; set; }
        public string EmsName { get; set; }

        public virtual ICollection<EmsType> EmsTypes { get; set; }
    }

调节器

public ActionResult Create()
    {
        CorporateDetails corporatedetails = new CorporateDetails();
        ViewBag.EmsId = new MultiSelectList(db.EmsTypes,"EmsId","EmsName");
        return View(corporatedetails);
    }

扩展(放置在项目根目录中的文件夹中)

public static MvcHtmlString CheckBoxListFor<TModel,TProperty>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel,TProperty[]>> expression,MultiSelectList multiSelectList,object htmlAttributes = null)
    {
        //Derive property name for checkBox name
        MemberExpression body = expression.Body as MemberExpression;
        string propertyName = body.Member.Name;

        //Get currently select values from the ViewData model
        TProperty[] list = expression.Compile().Invoke(htmlHelper.ViewData.Model);

        //Convert selected value list to a List<string> for easy manipulation
        List<string> selectedValues = new List<string>();

        if (list != null)
        {
            selectedValues = new List<TProperty>(list).ConvertAll<string>(delegate(TProperty i) { return i.ToString(); });
        }

        //Create div
        TagBuilder divTag = new TagBuilder("div");
        divTag.MergeAttributes(new RouteValueDictionary(htmlAttributes),true);

        //Add checkBoxes
        foreach (SelectListItem item in multiSelectList)
        {
            divTag.InnerHtml += String.Format("<div><input type=\"checkBox\" name=\"{0}\" id=\"{0}_{1}\" " +
                                                "value=\"{1}\" {2} /><label for=\"{0}_{1}\">{3}</label></div>",propertyName,item.Value,selectedValues.Contains(item.Value) ? "checked=\"checked\"" : "",item.Text);
        }

        return MvcHtmlString.Create(divTag.ToString());
    }

扩展名注册在视图的Web配置中

<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization"/>
    <add namespace="System.Web.Routing" />
    <add namespace="MyProject.Extensions" />
  </namespaces>
</pages>

视图

@model Valpak.Websites.HealthChecker.Models.CorporateDetails
@{
    ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>CorporateDetails</legend>

           <div class="editor-label">
           @Html.CheckBoxListFor(model => model.EmsId,(MultiSelectList) ViewBag.EmsId)
          </div>          
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List","Index")
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

这给我一个可爱的复选框列表.欢呼!

感谢Darin你的帮助,我已经把这个标记为答案,但是你的时间和精力是50.

猜你在找的asp.Net相关文章