MVC 动态生成 DropDownList
这是我的 ViewModel,
公共类 FormGroupsAssociationViewModel : BaseViewModel {
public FormGroupsAssociationViewModel()
{
FormTypeFormGroupViewModels = new List<FormTypeFormGroupViewModel>();
}
public FormGroupsAssociationViewModel(IList<FormTypeFormGroupViewModel> formTypeFormGroupViewModels)
{
this.FormTypeFormGroupViewModels = formTypeFormGroupViewModels;
}
public IList<FormTypeFormGroupViewModel> FormTypeFormGroupViewModels { get; set; }
}
公共类 FormTypeFormGroupViewModel {
public FormTypeFormGroupViewModel()
{
}
public FormTypeFormGroupViewModel(FormType formType, IEnumerable<FormGroup> formGroups)
{
this.FormTypeId = formType.FormTypeID;
this.Title = formType.Title;
this.FormGroups = new SelectList(formGroups, "FormGroupId", "Title", "3");
}
public int FormTypeId;
public string Title;
public SelectList FormGroups;
public int SelectedFormGroup;
}
视图如下:
@for (int i = 0; i < Model.FormTypeFormGroupViewModels.Count; i++)
{
<tr>
<td>
@Model.FormTypeFormGroupViewModels[i].Title
</td>
<td>
@Html.DropDownListFor(model => Model.FormTypeFormGroupViewModels[i].SelectedFormGroup, Model.FormTypeFormGroupViewModels[i].FormGroups)
</td>
</tr>
}
最后控制器是:
[HttpPost]
[UnitOfWork]
public String Associations(FormGroupsAssociationViewModel model)
{
String s = string.Empty;
foreach (var formTypeFormGroupViewModel in model.FormTypeFormGroupViewModels)
{
s = s + formTypeFormGroupViewModel.SelectedFormGroup.ToString();
}
return s;
}
我遇到的问题是 SelectedFormGroup 返回 0 而不是实际的 selectedValue。如果有人有任何想法,他们将不胜感激。
干杯
加雷斯
Here is my ViewModel,
public class FormGroupsAssociationViewModel : BaseViewModel
{
public FormGroupsAssociationViewModel()
{
FormTypeFormGroupViewModels = new List<FormTypeFormGroupViewModel>();
}
public FormGroupsAssociationViewModel(IList<FormTypeFormGroupViewModel> formTypeFormGroupViewModels)
{
this.FormTypeFormGroupViewModels = formTypeFormGroupViewModels;
}
public IList<FormTypeFormGroupViewModel> FormTypeFormGroupViewModels { get; set; }
}
public class FormTypeFormGroupViewModel
{
public FormTypeFormGroupViewModel()
{
}
public FormTypeFormGroupViewModel(FormType formType, IEnumerable<FormGroup> formGroups)
{
this.FormTypeId = formType.FormTypeID;
this.Title = formType.Title;
this.FormGroups = new SelectList(formGroups, "FormGroupId", "Title", "3");
}
public int FormTypeId;
public string Title;
public SelectList FormGroups;
public int SelectedFormGroup;
}
and heres the view:
@for (int i = 0; i < Model.FormTypeFormGroupViewModels.Count; i++)
{
<tr>
<td>
@Model.FormTypeFormGroupViewModels[i].Title
</td>
<td>
@Html.DropDownListFor(model => Model.FormTypeFormGroupViewModels[i].SelectedFormGroup, Model.FormTypeFormGroupViewModels[i].FormGroups)
</td>
</tr>
}
and finally the controller is:
[HttpPost]
[UnitOfWork]
public String Associations(FormGroupsAssociationViewModel model)
{
String s = string.Empty;
foreach (var formTypeFormGroupViewModel in model.FormTypeFormGroupViewModels)
{
s = s + formTypeFormGroupViewModel.SelectedFormGroup.ToString();
}
return s;
}
The problem i've got is the SelectedFormGroup is returning 0 rather than the actual selectedValue. If anyone has any ideas they would be greatly appreciated.
Cheers
Gareth
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了这个问题,
在我的 ViewModel 中,SelectedFormGroup 声明如下:
需要更改为:
Solved this,
In my ViewModel, the SelectedFormGroup was declared like this:
This needed to be changed to: