【问题标题】:How to populate a textbox based on dropdown selection in MVC..?如何根据 MVC 中的下拉选择填充文本框..?
【发布时间】:2013-05-24 17:55:03
【问题描述】:

您好,我创建了一个表并通过 ADO.NET 实体将其连接到 MVC 项目。 连接后,我为实体添加了控制器,它在 MVC 项目的 VIEW 文件夹中创建了一组 cshtml 文件。 但现在我需要的是创建一个下拉列表和文本框。 我在一个 cshtml 文件中创建了下拉列表,并在 CONTROLLER 中为其编写了逻辑。 我也可以创建 TEXTBOXES,但我面临基于下拉列表选择填充 TEXTBOX 的问题。

我的 VS 2012 自动生成的模型是

 public partial class Plan_S  

    {

        public int PlanId_PK { get; set; }
        public string PlanNames { get; set; }
        public string Hours { get; set; }
    }

用于显示下拉列表的控制器是 `

 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());
        }

    }`

我用于显示下拉列表的 view.cshtml 是

`

@model Pivot.Models.Plan_S
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


<div>   

    @Html.DropDownList("PlanNames", "--select--")

</div>

`

现在,当我在下拉列表中选择一个项目时,它应该会自动填充表中的相应值。 在我的代码中,Plan_S 表自动生成为 Plan_S 模型类。但在数据库中,我为表中的这些列设置了一组值。

eg..)     PlanId_PK  |   PlanNames  |    Hours
              1           Plan1          1hrs
              2           Plan2          2hrs
              3           Plan3          3hrs

在这个 Plan_S 表中,

PlanNames 列填充在 DROPDOWNLIST 中, 当我在 DDL 中选择 Plan1 时,它应该将 texbox 填充为 1hrs

当我在 DDL 中选择 Plan2 时,它应该将文本框填充为 2 小时。

这是我需要的逻辑,我可以在 asp webforms 中执行此操作,但在 MVC 中很棘手。

我认为它需要Jquery.......

请帮帮我,我花了好几个小时才找到这个逻辑......

提前谢谢...

【问题讨论】:

    标签: c# jquery asp.net-mvc asp.net-mvc-4 razor


    【解决方案1】:

    首先,让我们创建一个视图模型来保存这些东西:

    public class PlanViewModel
    {
        public List<SelectListItem> Plans { get; set; }
    }
    

    然后,在您的控制器操作中,让我们构建模型:

    public ActionResult Index()
    {
        var model = new PlanViewModel();
    
        model.Plans = db.Plan_S
            .Select(p => new SelectListItem
            {
                Value = p.Hours,
                Text = p.PlanNames
            })
            .ToList();
    
            return View(model);
        }
    

    然后在你的视图中,做:

    @model Pivot.Models.Plan_S
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    <div>   
        @Html.DropDownList("PlanNames", Model.Plans, "--select--")
        <input id="planHours" type="text" />
    </div>
    

    然后你需要在 jQuery 中执行以下操作:

    <script type="text/javascript">
        $(function () {
            $("[name='PlanNames']").change(function () {
                $("#planHours").val($(this).val());
            });
        });
    </script>
    

    【讨论】:

    • 嗨 mattytommo 我无法理解 Index() 方法中的 CurrentPlan...请在这方面帮助我...
    • @user2309770 你的页面的目的是什么?是编辑计划,创建计划吗?还是只显示信息?
    • 在下拉列表中单击 Plan1 时,文本框应填充表格中的相应值。在这里,单击 Plan1 时,文本框应填充为 1hrs,如我的问题中所示。 .
    • @user2309770 检查我更新的编辑。我以为您正在编辑计划,而不仅仅是显示信息。新代码将为您提供。
    • @user2309770 我猜你在这两个方面都有时间和成本是吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 2020-12-12
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多