【问题标题】:MVC, CheckboxList extension html helper..further issuesMVC, CheckboxList extension html helper..进一步的问题
【发布时间】:2012-06-27 20:28:15
【问题描述】:

下午好

我希望这会很好,我自己一个简单的他妈的,但我已经深入了,目前不会游泳。

好的,之前我问过this 问题,因此我已经实现了以下内容:

型号

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

控制器

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

查看

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

此 CheckBoxListFor 使用以下扩展名

public static class HtmlHelper
{
    //Extension
    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());
    }
}

那么有什么问题?

好吧,当我运行它时,我收到一个令人讨厌的错误告诉我:

"CS0411: 方法的类型参数 'Extensions.HtmlHelper.CheckBoxListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Web.Mvc.MultiSelectList, object)' 不能从 用法。尝试显式指定类型参数。”

有人可以指出我哪里出错了,因为我在兜圈子,不确定我错过了什么。

感谢任何帮助。

【问题讨论】:

    标签: asp.net-mvc razor checkbox asp.net-mvc-4


    【解决方案1】:

    这里的问题是我原来的 EmsId 是一个可为空的 int 而不是所需的 int 数组,因此是错误的。

    请参阅我给 here 的回答,了解如何实现这一点的完整说明。

    【讨论】:

      【解决方案2】:

      里卡多,

      我在 GitHub 上分享了一些我自己的 MVC 助手。请随意浏览并根据需要使用代码。项目中的一项是 CheckBoxListItemFor 助手,它是专门为创建复选框列表而创建的。在 InputExtensions.cs 下查看

      https://github.com/EdCharbeneau/EJC.UIExtensions/tree/master/EJC.UIExtensions/Html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-09
        • 1970-01-01
        相关资源
        最近更新 更多