How to populate a textbox based on dropdown selection in MVC..?

后端 未结 1 888
忘掉有多难
忘掉有多难 2021-01-15 06:53

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 file

1条回答
  •  猫巷女王i
    2021-01-15 07:25

    Firstly, let's create a View Model to hold these things:

    public class PlanViewModel
    {
        public List Plans { get; set; }
    }
    

    Then, in your controller action let's build the Model:

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

    Then in your View, do:

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

    Index

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

    Then you'll need to do the following in jQuery:

    
    

    0 讨论(0)
提交回复
热议问题