Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the logic is really simple as you shown above then there is no problem of duplicating the code in JavaScript as done by @Jonas. Sometimes we have to break the rules(DRY) to achieve something that gives a better experience to the user. If you don't duplicate the code then you are making an additional request to the server and the bird will fly away before knowing it's size :)</p> <p>But still if you want to do then..</p> <p><strong>Models</strong></p> <pre><code>public class BirdViewModel { public Bird Bird { get; set; } public IEnumerable&lt;SelectListItem&gt; Heights { get; set; } public IEnumerable&lt;SelectListItem&gt; Weights { get; set; } } public class Bird { public int WeightId { get; set; } public int HeightId { get; set; } public string Weight { get; set; } public string Height { get; set; } public string BirdSize { get { if (HeightId == 3 &amp;&amp; WeightId == 3) { return "Big"; } else { return "Small"; } } } } </code></pre> <p><strong>Controller</strong></p> <pre><code>public class BirdController : Controller { public ActionResult Index() { var model = new BirdViewModel(); model.Weights = new[] { new SelectListItem{ Text = "Light", Value = "1" }, new SelectListItem{ Text = "Medium", Value = "2" }, new SelectListItem{ Text = "Heavy", Value = "3" } }; model.Heights = new[] { new SelectListItem{ Text = "Short", Value = "1" }, new SelectListItem{ Text = "Medium", Value = "2" }, new SelectListItem{ Text = "Tall", Value = "3" } }; return View(model); } [HttpPost] public PartialViewResult DisplaySize(Bird bird) { return PartialView(bird); } } </code></pre> <p><strong>Index.cshtml</strong></p> <pre><code>@model Birds.Models.BirdViewModel @{ ViewBag.Title = "Index"; var ajaxOptions = new AjaxOptions { UpdateTargetId = "birdSize", Url = "Bird/DisplaySize" }; } &lt;h2&gt;Index&lt;/h2&gt; @using (Ajax.BeginForm(ajaxOptions)) { &lt;h3&gt;Bird Weight&lt;/h3&gt; &lt;div&gt; @Html.DropDownListFor(model =&gt; model.Bird.WeightId, Model.Weights, new { @id = "weightddl" }) &lt;/div&gt; &lt;h3&gt;Bird Height&lt;/h3&gt; &lt;div&gt; @Html.DropDownListFor(model =&gt; model.Bird.HeightId, Model.Heights, new { @id = "heightddl" }) &lt;/div&gt; &lt;h3&gt; Bird Size&lt;/h3&gt; &lt;div id="birdSize"&gt; @Html.Partial("DisplaySize", Model.Bird ?? new Birds.Models.Bird()) &lt;/div&gt; } &lt;script type="text/javascript"&gt; $("select").change(function () { $("form").submit(); }); &lt;/script&gt; </code></pre> <p><strong>DisplaySize.cshtml</strong></p> <pre><code>@model Birds.Models.Bird @Html.DisplayFor(model =&gt; model.BirdSize) </code></pre> <p>I've used <code>Ajax.BeginForm</code> so you have to include the <code>jquery.unobtrusive-ajax.min.js</code> library.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      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