Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to populate a textbox based on dropdown selection in MVC..?
    text
    copied!<p>Hi I have created a table and connected it to MVC project through ADO.NET entity. After connecting I added the controller for the entity and it creates a set of cshtml files in VIEW folder in MVC project. But now What I need is to create a dropdownlist and textbox. I created the dropdownlist in a cshtml file and also wriiten the logic for it in the CONTROLLER. I can also create TEXTBOXES,but i'm facing the problem of poulating TEXTBOX based on the dropdownlist selection.</p> <p>My Model auto generated by VS 2012 is </p> <pre><code> public partial class Plan_S { public int PlanId_PK { get; set; } public string PlanNames { get; set; } public string Hours { get; set; } } </code></pre> <p>My Controller for displaying dropdownlist is `</p> <pre><code> public class dropdownController : Controller { private PivotEntities db = new PivotEntities(); // // GET: /dropdown/ public ActionResult Index() { ViewBag.plannames = new SelectList(db.Plan_S, "PlanId_PK", "PlanNames"); return View(); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } public ActionResult ddl() { return View(new Plan_S()); } }` </code></pre> <p>My view.cshtml for displaying dropdownlist is </p> <p>`</p> <pre><code>@model Pivot.Models.Plan_S @{ ViewBag.Title = "Index"; } &lt;h2&gt;Index&lt;/h2&gt; &lt;div&gt; @Html.DropDownList("PlanNames", "--select--") &lt;/div&gt; </code></pre> <p>`</p> <p>Now When I select an item in the dropdownlist, it should automatically populate the corresponding value in the table. Here in my code, Plan_S table is autogenrated as Plan_S MODEL Class. But in Database I have set of values for these columns in table.</p> <pre><code>eg..) PlanId_PK | PlanNames | Hours 1 Plan1 1hrs 2 Plan2 2hrs 3 Plan3 3hrs </code></pre> <p>Here in this Plan_S table,</p> <p>PlanNames column is populated in the DROPDOWNLIST, When I select the Plan1 in DDL it should populate the texbox as 1hrs</p> <p>When I select the Plan2 in DDL it should populate the textbox as 2hrs.</p> <p>This is the logic i need and I can do this in asp webforms but it is tricky in MVC.</p> <p>I think that Jquery is needed for it.......</p> <p>Please help me, I had spent hours in finding this logic....</p> <p>Thanks in advance...</p>
 

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