Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get DropDownList items when submit?
    primarykey
    data
    text
    <p>I have the following details in my project</p> <p><strong>MODEL</strong></p> <pre><code> public class AnimalModels { public string AnimalId { get; set; } public List&lt;SelectListItem&gt; AnimalList { get; set; } } </code></pre> <p><strong>VIEW</strong></p> <pre><code>@model DropDownList.Models.AnimalModels @{ ViewBag.Title = "Index"; } &lt;h2&gt;Index&lt;/h2&gt; &lt;script src="@Url.Content("~/Scripts/Animal/Index.js")" type="text/javascript"&gt;&lt;/script&gt; @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;div&gt; &lt;fieldset&gt; &lt;div&gt;@Html.LabelFor(model =&gt; model.AnimalId)&lt;/div&gt; &lt;div&gt;@Html.DropDownListFor(model =&gt; model.AnimalId, Model.AnimalList)&lt;/div&gt; &lt;div&gt;@Html.ValidationMessageFor(model =&gt; model.AnimalId)&lt;/div&gt; &lt;/fieldset&gt; &lt;input type="submit" name="Submit" value="Submit" /&gt; &lt;/div&gt; } </code></pre> <p><strong>CONTROLLER</strong></p> <pre><code> public class AnimalController : Controller { // // GET: /Animal/ public ActionResult Index() { AnimalModels model = new AnimalModels(); model.AnimalList = new List&lt;SelectListItem&gt;(); return View(model); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(AnimalModels model) { //TODO: get animal list model.AnimalList = new List&lt;SelectListItem&gt;(); return View(model); } } </code></pre> <p><strong>Index.js</strong></p> <pre><code>//[B]Document ready $(document).ready(function () { //Populate ddl using ajax (jquery) $.ajax({ url: 'Resources/Animals.xml', // name of file with our data - link has been renamed dataType: 'xml', // type of file we will be reading success: parse, // name of function to call when done reading file error: showError // name of function to call when failed to read }); }); //[E]Document ready function parse(xmlDoc) { var options = new Array(); $(xmlDoc).find("ITEMS").each(function () { $(this).find("ITEM").each(function () { var optionValue = $(this).find('VALUE').text(); var optionLabel = $(this).find('TEXT').text(); options.push('&lt;option value="' + optionValue + '"&gt;' + optionLabel + '&lt;/option&gt;'); }); }); options = options.join(''); $('#AnimalId').append(options); } var showError = function (xhr, status, err) { alert("Error loading file Animals.xml in Resources folder.\n" + xhr.status + " " + err + "."); }; </code></pre> <p><strong>Animals.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;ITEMS&gt; &lt;ITEM&gt; &lt;VALUE&gt;-1&lt;/VALUE&gt; &lt;TEXT&gt;&lt;/TEXT&gt; &lt;/ITEM&gt; &lt;ITEM&gt; &lt;VALUE&gt;0&lt;/VALUE&gt; &lt;TEXT&gt;Tiger&lt;/TEXT&gt; &lt;/ITEM&gt; &lt;ITEM&gt; &lt;VALUE&gt;1&lt;/VALUE&gt; &lt;TEXT&gt;Lion&lt;/TEXT&gt; &lt;/ITEM&gt; &lt;/ITEMS&gt; </code></pre> <p>my question is there any way to get all drop down list items when i hit submit button? because the value is always null when i tried to debug it</p> <p><img src="https://i.stack.imgur.com/Fy5lq.png" alt="enter image description here"></p>
    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