Note that there are some explanatory texts on larger screens.

plurals
  1. POC# MVC4: Replace <div> when selection is made from a Html.DropDownList using jQuery
    primarykey
    data
    text
    <p>I need to be able to populate data into a <code>&lt;div&gt;</code> or some other sort of section from an object after the corresponding string has been selected from a drop down list (lazy loading). </p> <p>When a chnage is made in the dropdownlist, I want the method in my controller to be called which will fill in <code>&lt;div id=result&gt;&lt;/div&gt;</code> with the output from the method.</p> <p>Perhaps I am approaching this problem wrong. I suspect the problem is in my JavaScript. Here is my approach:</p> <p>View:</p> <pre><code> &lt;div&gt;@Html.DropDownList("MyDDL") &lt;/div&gt; &lt;br&gt; &lt;div id="result"&gt;&lt;/div&gt; </code></pre> <p>JavaScript:</p> <pre><code>&lt;script type ="text/javascript"&gt; $(document).ready(function () { $("#MyDDL").change(function () { var strSelected = ""; $("#MyDDL option:selected").each(function () { strSelected += $(this)[0].value; }); var url = "HomeController/showInfo"; //I suspect this is not completely correct: $.post(url, {str: strSelected},function (result) { $("result").html(result); }); }); }); &lt;/script&gt; </code></pre> <p>Controller (Perhaps I shouldn't be using <code>PartialViewResult</code>):</p> <pre><code>public ActionResult Index() { List&lt;string&gt; myList = new List&lt;string&gt;(); List&lt;SelectListItem&gt; MyDDL = new List&lt;SelectListItem&gt;(); myList.Add("Tim"); myList.Add("Joe"); myList.Add("Jim"); //fill MyDDL with items from myList MyDDL = myList .Select(x =&gt; new SelectListItem { Text = x, Value = x }) .ToList(); ViewData["MyDDL"] = MyDDL; return View(); } [HttpPost] public PartialViewResult showInfo(string str) { Person p = new Person(str); //name is passed to constructor p.LoadInfo(); //database access in Person Model ViewBag.Info = p.Info; return PartialView("_result"); } </code></pre> <p>_result.cshtml:</p> <pre><code>&lt;p&gt; @ViewBag.Info &lt;/p&gt; </code></pre> <p>Thanks You.</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.
    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