asp.net mvc 多选记住回发后的状态
我正在使用 DataAnnotations 对我的 asp.net mvc 应用程序进行错误检查,我也使用强类型 ViewModel。
我的错误检查工作正常,如果字段为空,则会将错误消息发回我的视图。但是,我的表单上有一个多选/列表框,我需要记住它在发生错误后的状态。
目前我的 ViewModel 看起来像这样(我只包含相关属性):
public class ProfilePageViewModel : PageViewModel
{
public IList<FavouriteGenreViewModel> FavGenres { get; set; }
[Required(ErrorMessage = "*")]
public string SelectedGenres { get; set; }
public IDictionary<int, string> GenresList { get; set; }
}
这是我控制器中的操作:
public ActionResult Profile(ProfilePageViewModel viewModel)
{
if(!ModelState.IsValid)
{
viewModel.CountriesList = dropDownTasks.GetCountries();
viewModel.GendersList = dropDownTasks.GetGenders();
viewModel.GenresList = dropDownTasks.GetGenres();
viewModel.TimezonesList = dropDownTasks.GetTimezones();
viewModel.FavGenres =
return View(viewModel);
}
. . .
我的 MultiSelect 采用 FavouriteGenreViewModel 列表来选择 GenresList 中的选项,它在 GET 操作中使用 AutoMapper 来执行此操作,但是显然我不能在帖子上使用 AutoMapper,因为它会忘记我发布的值。
我考虑过使用逗号分隔的 ID 字符串而不是 FavouriteGenreViewModel 列表,这样我就可以重新使用回发后的值...但是我希望有人有一种更优雅的方式来处理这个问题。
谢谢你!
保罗
I am using the DataAnnotations for error checking on my asp.net mvc app, I am also using strongly typed ViewModels too.
My error checking is working fine and is posting back to my view with error messages if a field is blank. However i have a MultiSelect / Listbox on my form which i need to remember it's state after an error.
At the moment my ViewModel looks like this (i have only included relevant properties):
public class ProfilePageViewModel : PageViewModel
{
public IList<FavouriteGenreViewModel> FavGenres { get; set; }
[Required(ErrorMessage = "*")]
public string SelectedGenres { get; set; }
public IDictionary<int, string> GenresList { get; set; }
}
This is my Action in my controller:
public ActionResult Profile(ProfilePageViewModel viewModel)
{
if(!ModelState.IsValid)
{
viewModel.CountriesList = dropDownTasks.GetCountries();
viewModel.GendersList = dropDownTasks.GetGenders();
viewModel.GenresList = dropDownTasks.GetGenres();
viewModel.TimezonesList = dropDownTasks.GetTimezones();
viewModel.FavGenres =
return View(viewModel);
}
. . .
My MultiSelect takes a list of FavouriteGenreViewModel's to select the options in the GenresList, it does this using AutoMapper in GET action, but obviouslly i can't use AutoMapper on the post because it will forget my posted values.
I have thought about using a comma delimmated string of ID's instead of a list of FavouriteGenreViewModel's, that way i can re-use the value once posted back...however i am hoping someone has a more elegant way of dealing with this problem.
Thank you!
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想经过一番探索后我可以回答我自己的问题。
在我的 ViewModel 中,我使用字符串数组作为数据类型,如下所示:
在我看来,我可以将选定的值设置为 FavGenres,然后我有一个自定义模型绑定器,可以将逗号分隔的字符串再次转换为有效对象...以防万一我想知道这是我的自定义模型活页夹的完整内容......
I think i can answer my own question after some poking around.
In my ViewModel i use a string array as the Data Type like so:
In my view i can set the selected values to FavGenres, then i have a custom model binder to convert the comma seperated string into valid objects again...in case you are wondering here is my custom model binder in full...