Note that there are some explanatory texts on larger screens.

plurals
  1. POBind a ViewModel to a DropDownListFor with a third value besides dataValueField/dataTextField
    primarykey
    data
    text
    <p>When I show a list of testplanViewModels in my View and the user selects one the SelectedTestplanId is returned to the Controller post action. What should also be returned is the TemplateId which belongs to the SelectedTestplanId.</p> <p>When the AutoMapper definition is run the <strong>Testplan.TestplanId</strong> is implicitly copied over to the <strong>TestplanViewModel.TestplanId</strong>. The same could be done by providing a <strong>TemplateId</strong> on the TestplanViewModel. When the user selects now a "TestplanViewModel" in the View, how can I attach the TemplateId to the controller action to access it there? The DropDownList does not allow 2 dataValueFields!</p> <pre><code>CreateMap&lt;Testplan, TestplanViewModel&gt;().ForMember(dest =&gt; dest.Name, opt =&gt; opt.MapFrom(src =&gt; string.Format("{0}-{1}-{2}-{3}", src.Release.Name, src.Template.Name, src.CreatedAt, src.CreatedBy))); public ActionResult OpenTestplanViewModels() { IEnumerable&lt;Testplan&gt; testplans = _testplanDataProvider.GetTestplans(); var viewModel = new OpenTestplanViewModel { DisplayList = Mapper.Map&lt;IEnumerable&lt;Testplan&gt;, IEnumerable&lt;TestplanViewModel&gt;&gt;(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&lt;TestplanViewModel&gt; DisplayList { get; set; } } </code></pre> <p><strong>OpenTestplanViewModel</strong></p> <pre><code>@using (Html.BeginForm("Open", "Testplan")) { @Html.ValidationSummary(false) @Html.DropDownListFor(x =&gt; x.SelectedTestplanId, new SelectList(Model.DisplayList, "TestplanId", "Name"), new { @class = "listviewmodel" }) } </code></pre> <p><strong>Solution</strong>:</p> <pre><code> public class OpenTestplanViewModel { [Required(ErrorMessage = "No item selected.")] public string TestplanIdAndTemplateId { get; set; } public IEnumerable&lt;TestplanViewModel&gt; 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&lt;Testplan, TestplanViewModel&gt;() .ForMember(d =&gt; d.Name, o =&gt; o.MapFrom(src =&gt; string.Format("{0}-{1}-{2}-{3}", src.Release.Name, src.Template.Name, src.CreatedAt, src.CreatedBy))) .ForMember(d =&gt; d.TestplanIdAndTemplateId, o =&gt; o.MapFrom(src =&gt; src.TestplanId + "_" + src.TemplateId)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload