【问题标题】:Super simple implementation of multiselect list box in Edit view编辑视图中多选列表框的超简单实现
【发布时间】:2013-08-24 03:55:14
【问题描述】:

在这里将 MVC4 与 EF 和 CF 一起使用(非常糟糕)

我有这样的课:

public class Feature
{
    public int ID { get; set; }
    public string Desc { get; set; }
}

还有这样的:

public class Device   //change to Devices
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Feature> Features { get; set; }
}

在 Device 模型的 Edit 视图中,我希望有一个 ListBox,其中包含 Feature 模型的所有元素(显示的 Desc 属性),并且这些特征包含在 device.Features 集合中预先选择。

然后,当用户在 Edit 视图上单击 Save 时,ListBox 中所选项目的当前集合被写回到设备的 Features 集合中。

这个技巧的控制器代码和 cshtml 是什么样的?

感谢您的宝贵时间, 戴夫

【问题讨论】:

    标签: asp.net-mvc-4 ef-code-first


    【解决方案1】:

    与往常一样,您可以从编写满足您描述的视图要求的视图模型开始:

    public class EditDeviceViewModel
    {
        public IEnumerable<int> SelectedFeatures { get; set; }
        public IEnumerable<SelectListItem> Features { get; set; }
        public int ID { get; set; }
        public string Name { get; set; }
    }
    

    然后是你的控制器:

    public class DeviceController : Controller
    {
        public ActionResult Edit(int id)
        {
            Device device = (go get your device from your repository using the id)
            IList<Feature> features = (go get all features from your repository)
    
            // now build the view model
            var model = new EditDeviceViewModel();
            model.ID = device.ID;
            model.Name = device.Name;
            model.SelectedFeatures = device.Features.Select(x => x.ID);
            model.Features = features
                .Select(x => new SelectListItem
                {
                    Value = x.ID.ToString(),
                    Text = x.Name,
                })
                .ToList();
    
            // pass the view model to the view
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Edit(EditDeviceViewModel model)
        {
            // model.SelectedFeatures will contain the selected feature IDs here
            ...
        }
    }
    

    最后是视图:

    @model EditDeviceViewModel
    
    @using (Html.BeginForm())
    {
        @Html.Html.HiddenFor(x => x.ID)
        <div>
            @Html.LabelFor(x => x.Name)
            @Html.EditorFor(x => x.Name)
        </div>
        <div>
            @Html.LabelFor(x => x.SelectedFeatures)
            @Html.ListBoxFor(x => x.SelectedFeatures, Model.Features)
        </div>
    
        <button type="submit">Edit</button>
    }
    

    【讨论】:

    • 当我加载视图时,设备集合中已有的任何功能都不会在列表中预先选择,每当我点击列表中的一个项目时,我都会收到一个 jScript 错误:未处理的异常在localhost:52321/Scripts/jquery.validate.js 0x800a138f 中的第 1234 行第 5 列 - JavaScript 运行时错误:无法获取未定义或空引用的属性“调用”
    • 是的,你是对的,我在控制器操作中忘记了以下内容:model.SelectedFeatures = device.Features.Select(x =&gt; x.ID);。这将预先选择设备中存在的值。至于你得到的 javascript 错误,嗯,这是你认为的,你没有显示的代码,所以我无法读懂你的想法并说出你为什么会得到这个错误。也许你在某处或某处遇到了一些 javascript 错误。
    • 我没有对 jscript 做过任何我知道的事情,这开始是作为一个股票 MVC4 VS 生成的应用程序。如果我什至尝试滚动列表框,它也会引发同样的错误。我看到框中的项目在 VS 出现错误之前开始滚动。
    • 在这个应用程序生命周期的早期,我一直在尝试应用 Themeroller 主题,但显然在 jScript 中搞砸了。我把它烧到地上并开始清理,你的解决方案工作正常。谢谢!
    • @DarinDimitrov - 当我尝试模拟这一点时,我发布的模型的成员为空 - 即 model.SelectedFeatures 为空。但是,当我查看 Request.Params 时 - 所有 SelectedFeatures 都会返回那里。为什么它们不在我的模型中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多