Note that there are some explanatory texts on larger screens.

plurals
  1. POCombining Razor, Ajax, JSON
    primarykey
    data
    text
    <p>I try to make following parts run, but always failed. The objective is: if a target in combobox is selected, the mediaId's combobox should be filled with respective values. At this moment I just emulate the values of mediaId combobox. Can anyone show me how to combine them correctly? Thx in advance.</p> <p>The view Medium.cshtml:</p> <pre><code>&lt;script src="@Url.Content("~/Scripts/PartialLoad.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Files[i].TargetId) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownListFor(model =&gt; model.Files[i].PTargetId, (ViewData["targets"] as SelectList).MakeSelection(Model.Files[i].PTargetId)) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Files[i].MediaId) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownListFor(model =&gt; model.Files[i].MediaId, (ViewData["mediaIds"] as SelectList).MakeSelection(1)) &lt;/div&gt; </code></pre> <p>The javascript partialload.js</p> <pre><code>$(document).ready(function () { $("#targets").change(function () { GetMValues("#targets", "#mediaIds"); }); }); function ClearDrop(objSource) { $(objSource).empty(); } function GetMValues(objSource, objDest) { var url = '/GetMValues/'; $.getJSON(url, { id: $(objSource).val() }, function (data) { ClearDrop(objDest); $.each(data, function (index, optionData) { $(objDest).append("&lt;option value='" + optionData.Value + "'&gt;" + optionData.Text + "&lt;/option&gt;"); }); }); } </code></pre> <p>The homecontroller.cs</p> <pre><code>public ActionResult GetMValues(String id) { int myId = 0; int.TryParse(id, out myId); var mediumIds = new List&lt;long&gt;(); int max = myId + 3; // just to emulate the data in the list for ( long l = 1 ; l &lt; max ; l++ ){ mediumIds.Add(l); } var select = new SelectList(mediumIds, "PTargetId", "TargetId"); return Json(select, JsonRequestBehavior.AllowGet); //allow get needed to allow get calls } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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