Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update a property on an MVC view using jquery
    primarykey
    data
    text
    <p>I have an MVC view that has a property BirdSize for a Bird on it.</p> <p>When the weight or height select lists of the bird changes this BirdSize property's value can potentially change.</p> <p>What I'm wanting is for this update to happen without complete screen refresh so jquery I guess.</p> <p>I don't particularly want to duplicate the BirdSize property code but just want to use it as is.</p> <p>So the class:</p> <pre><code>public class Bird { public int WeightId { get; set; } public Weight Weight { get; set; } public int HeightId { get; set; } public Height Height { get; set; } public string BirdSize { if(Height == "Tall" &amp;&amp; Weight == "Heavy") { return "Big"; } else { return "Small"; } } } </code></pre> <p>Then I have a View:</p> <pre><code>@model Ahb.Insite.HerdRegistration.WebUI.ViewModels.BirdModel &lt;script src="../../Scripts/jquery-1.7.2.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="../../Scripts/Views/HerdRegistrationWizard/index.js" type="text/javascript"&gt;&lt;/script&gt; &lt;h3&gt;Bird Weight/h3&gt; &lt;div&gt; @Html.DropDownListFor(model =&gt; model.Bird.WeightId, Model.BirdWeightSelectListItems, new { @id = "weightddl" }) &lt;/div&gt; &lt;h3&gt;Bird Height/h3&gt; &lt;div&gt; @Html.DropDownListFor(model =&gt; model.Bird.HeightId, Model.BirdHeightSelectListItems, new { @id = "heightddl" }) &lt;/div&gt; &lt;h3&gt; Bird Size&lt;/h3&gt; &lt;div&gt; @Html.DisplayFor(model =&gt; model.Bird.BirdSize) &lt;/div&gt; </code></pre> <p>So the selects would look like:</p> <pre><code>&lt;select id="weightddl"&gt; &lt;option value="1"&gt;Light&lt;/option&gt; &lt;option value="2"&gt;Medium&lt;/option&gt; &lt;option value="3"&gt;Heavy&lt;/option&gt; &lt;/select&gt; &lt;select id="heightddl"&gt; &lt;option value="1"&gt;Short&lt;/option&gt; &lt;option value="2"&gt;Medium&lt;/option&gt; &lt;option value="3"&gt;Tall&lt;/option&gt; &lt;/select&gt; </code></pre> <p>So I guess I would do something in these script codes:</p> <pre><code>$('#weightddl').change(function() { //Do something to update birdsize }); $('#heightddl').change(function() { //Do something to update birdsize }); </code></pre> <p>I'm not sure the best way of going about it. Only way I can think of is to use the new values to be sent to a server method which will create a new bird and send back that bird's size. Anyone know of a better way to get this functionality working?</p> <p>Maybe you could use a partial view?</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.
 

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