[英]Bind a ViewModel to a DropDownListFor with a third value besides dataValueField/dataTextField
When I show a list of testplanViewModels in my View and the user selects one the SelectedTestplanId is returned to the Controller post action. 当我在视图中显示testplanViewModels列表并且用户选择一个列表时,SelectedTestplanId将返回到Controller后操作。 What should also be returned is the TemplateId which belongs to the SelectedTestplanId. 还应该返回的是属于SelectedTestplanId的TemplateId。
When the AutoMapper definition is run the Testplan.TestplanId is implicitly copied over to the TestplanViewModel.TestplanId . 运行AutoMapper定义时,将Testplan.TestplanId隐式复制到TestplanViewModel.TestplanId 。 The same could be done by providing a TemplateId on the TestplanViewModel. 通过在TestplanViewModel上提供TemplateId可以完成相同的操作。 When the user selects now a "TestplanViewModel" in the View, how can I attach the TemplateId to the controller action to access it there? 当用户现在在视图中选择一个“ TestplanViewModel”时,如何将TemplateId附加到控制器动作以在其中访问它? The DropDownList does not allow 2 dataValueFields! DropDownList不允许2个dataValueFields!
CreateMap<Testplan, TestplanViewModel>().ForMember(dest => dest.Name, opt => opt.MapFrom(src => string.Format("{0}-{1}-{2}-{3}", src.Release.Name, src.Template.Name, src.CreatedAt, src.CreatedBy)));
public ActionResult OpenTestplanViewModels()
{
IEnumerable<Testplan> testplans = _testplanDataProvider.GetTestplans();
var viewModel = new OpenTestplanViewModel
{
DisplayList = Mapper.Map<IEnumerable<Testplan>, IEnumerable<TestplanViewModel>>(testplans)
};
return PartialView(viewModel);
}
public class TestplanViewModel
{
public int TestplanId { get; set; }
public string Name { get; set; }
}
public class OpenTestplanViewModel
{
[Required(ErrorMessage = "No item selected.")]
public int SelectedTestplanId { get; set; }
public IEnumerable<TestplanViewModel> DisplayList { get; set; }
}
OpenTestplanViewModel OpenTestplanViewModel
@using (Html.BeginForm("Open", "Testplan"))
{
@Html.ValidationSummary(false)
@Html.DropDownListFor(x => x.SelectedTestplanId, new SelectList(Model.DisplayList, "TestplanId", "Name"), new { @class = "listviewmodel" })
}
Solution : 解决方案 :
public class OpenTestplanViewModel
{
[Required(ErrorMessage = "No item selected.")]
public string TestplanIdAndTemplateId { get; set; }
public IEnumerable<TestplanViewModel> DisplayList { get; set; }
public int SelectedTestplanId
{
get
{
return Convert.ToInt32(TestplanIdAndTemplateId.Split(new[] { '_' }).First());
}
}
public int SelectedTemplateId
{
get
{
return Convert.ToInt32(TestplanIdAndTemplateId.Split(new[] { '_' }).Last());
}
}
}
CreateMap<Testplan, TestplanViewModel>()
.ForMember(d => d.Name, o => o.MapFrom(src => string.Format("{0}-{1}-{2}-{3}", src.Release.Name, src.Template.Name, src.CreatedAt, src.CreatedBy)))
.ForMember(d => d.TestplanIdAndTemplateId, o => o.MapFrom(src => src.TestplanId + "_" + src.TemplateId));
HTML doesn't really work that way. HTML并不是真的那样工作。 If you want more than one value returned from the post for the dropdown (the helper generates a select
element), you'll have to create a property on your view model that you then parse within the controller. 如果您希望从帖子中为下拉列表返回多个值(帮助程序会生成一个select
元素),则必须在视图模型上创建一个属性,然后在控制器中进行解析。
For instance, if you had two integer ID
fields, the combined property could create a value that looks something like 23_42
. 例如,如果您有两个整数ID
字段,则Combined属性可以创建一个看起来像23_42
的值。 You could then use the Split
method to get the correct values ( 23
& 42
) back. 然后,您可以使用Split
方法来取回正确的值( 23
& 42
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.