@ html.Listbox(“someName”,Model.Items)有效,但是@ Html.EditorFor(model => model.items)不起作用么?

时间:2011-09-20 21:11:23

标签: c# asp.net-mvc-3

错误类型:为@ Html.EditorFor(model => model.Items)输出false false false

预期结果:具有用户名称的列表框。

ViewModel

public class NewVariance
    {
        MVRLinqDataContext LinqCtx = new MVRLinqDataContext()
        public NewVariance()
        {
            IndividualsObjectTest();//load data dropdown and listbox data
        }

        public MultiSelectList Items { get; set; }

        //loads the values of the MultiSelectList above
        public void IndividualsObjectTest()
        {
            var IndividualsDropDownList =
                        (
                        from x in LinqCtx.ViewIndividualDropDownBoxes
                        orderby x.FullName
                        select x);

               Items = new MultiSelectList
              (
                IndividualsDropDownList as 
System.Collections.IEnumerable, "First_Hospital_Case_Nbr", "FullName"

              );

        }

控制器

public class NewVarianceController : Controller
    {
        public ActionResult Index()
       {
              var model = new NewVariance();
              return View(new model);
        }

    }

查看

@Html.ListBox("d",Model.Items)   THIS WORKS 
    @Html.EditorFor(model => model.Items) THIS DOESNT WORK OUTPUTS FALSE FALSE FALSE 

1 个答案:

答案 0 :(得分:3)

编辑器模板只根据属性类型呈现一些default output。如果你希望做一些有用的事情,你需要个性化它。您可以在this blog post上阅读有关模板化助手的更多信息。您不能指望默认编辑器模板会知道您希望它为此给定属性呈现列表框。因此,您可以编写自定义编辑器模板并个性化此行为。