具有多选功能的 asp.net mvc 强类型视图模型

发布于 2024-08-17 02:15:07 字数 511 浏览 3 评论 0原文

我想知道如何将表单值从多选框绑定到强类型视图。

显然,当表单提交时,多选框将提交我选择的值的分隔字符串...将此值字符串转换回对象列表以附加到要更新的模型的最佳方法是什么?

public class MyViewModel {
    public List<Genre> GenreList {get; set;}
    public List<string> Genres { get; set; }
}

当在控制器内更新我的模型时,我使用如下所示的 UpdateModel:

Account accountToUpdate = userSession.GetCurrentUser();
UpdateModel(accountToUpdate);

但是我需要以某种方式将字符串中的值返回到对象中。

我相信这可能与模型绑定程序有关,但我找不到任何清晰的示例来说明如何做到这一点。

谢谢!! 保罗

I would like to know how i can bind my form values to my strongly typed view from a MultiSelect box.

Obviously when the form submits the multi-select box will submit a delittemered string of my values selected...what is the best way to convert this string of values back into a list of objects to attach to my model to be updated?

public class MyViewModel {
    public List<Genre> GenreList {get; set;}
    public List<string> Genres { get; set; }
}

When updating my model inside the controller i am using UpdateModel like below:

Account accountToUpdate = userSession.GetCurrentUser();
UpdateModel(accountToUpdate);

However i need to somehow get the values from the string back into objects.

I beleive it may have something to do with model-binders but i can't find any good clear examples of how to do this.

Thanks!!
Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻了热茶 2024-08-24 02:15:07

你是对的,模型活页夹是正确的选择。试试这个...

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

[ModelBinder(typeof(MyViewModelBinder))]
public class MyViewModel {
    ....
}

public class MyViewModelBinder : DefaultModelBinder {
    protected override void SetProperty(ControllerContext context, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value) {
        if (propertyDescriptor.Name == "Genres") {
            var arrVals = ((string[])value)[0].Split(',');
            base.SetProperty(context, bindingContext, propertyDescriptor, new List<string>(arrVals));
        }
        else
            base.SetProperty(context, bindingContext, propertyDescriptor, value);
    }
}

You're correct that a model binder is the way to go. Try this...

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

[ModelBinder(typeof(MyViewModelBinder))]
public class MyViewModel {
    ....
}

public class MyViewModelBinder : DefaultModelBinder {
    protected override void SetProperty(ControllerContext context, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value) {
        if (propertyDescriptor.Name == "Genres") {
            var arrVals = ((string[])value)[0].Split(',');
            base.SetProperty(context, bindingContext, propertyDescriptor, new List<string>(arrVals));
        }
        else
            base.SetProperty(context, bindingContext, propertyDescriptor, value);
    }
}
(り薆情海 2024-08-24 02:15:07

查看 Phil Haacks 博客文章主题。我在最近的项目中使用它作为多选强类型视图的基础。

Check out Phil Haacks blog post on the subject. I used that as the basis for a multi select strongly typed view in a recent project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文