具有多选功能的 asp.net mvc 强类型视图模型
我想知道如何将表单值从多选框绑定到强类型视图。
显然,当表单提交时,多选框将提交我选择的值的分隔字符串...将此值字符串转换回对象列表以附加到要更新的模型的最佳方法是什么?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是对的,模型活页夹是正确的选择。试试这个...
You're correct that a model binder is the way to go. Try this...
查看 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.