简体   繁体   English

我如何从下拉列表中选择值到模式或控制器MVC

[英]how do i select value from dropdownlist to mode or to controller mvc

Model 模型

public class Company
{
    public int ID { get; set; }
    ....
    public int WorkZoneID { get; set;  }
    public IEnumerable<SelectListItem> WorkZones { get; set; }

Controller: 控制器:

ViewBag.Workzones = cmpRep.GetWorkzoneDrop();

Repository: 仓库:

public List<SelectListItem> GetWorkzoneDrop()
{
    SqlDataReader DR;
    DR = Ado.ExecDataReaderProc("WorkZoneSelectActive");
    List<SelectListItem> selectWorkZoneListItems = new     List<SelectListItem>();
    CompanyVMList C = new CompanyVMList();
    C.Companies = new List<Company>();
    while (DR.Read())
    {
        SelectListItem selectListItem = new SelectListItem
        {
            Text = Convert.ToString(DR["Name"]),
            Value = Convert.ToString(DR["ID"]),
        };
        selectWorkZoneListItems.Add(selectListItem);
    }
    return selectWorkZoneListItems;
}

View 视图

@Html.DropDownList("Workzones")

I want to assign selected dropdownlist value to int work zone int the mode how do i select value from dropdownlist to model or to controller 我想将选定的dropdownlist值分配给int工作区int模式我如何从dropdownlist中选择值以建模或分配给控制器

You need to bind your dropdownlist to property WorkZoneID 您需要将下拉列表绑定到属性WorkZoneID

@Html.DropDownListFor(m => m.WorkZoneID, Model.Workzones)

and if your wanting to preselect an option, you need to set the value in the controller before you pass the model to the view. 如果要预选一个选项,则需要在控制器中设置值,然后再将模型传递给视图。 And since your model contains a property for the SelectList , then use it (don't use ViewBag ) 并且由于您的模型包含SelectList的属性,请使用它(不要使用ViewBag

Company model = new Company
{
    WorkZoneID = 1 // set to a value that matches one of your options
    Workzones = cmpRep.GetWorkzoneDrop();
};
return View(model);

and when you submit to 当你提交给

[HttpPost]
public ActionResult Edit(Company model)

the value of model.WorkZoneID will contain the value of the selected option. model.WorkZoneID的值将包含所选选项的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从ASP.NET MVC和C#中的数据库中为下拉列表选择值? - How do I select a value for a dropdownlist from a database in ASP.NET MVC and C#? 如何将数据从Select2选项/值(标签)获取到ASP.NET MVC中的控制器? - How do I get the data from a Select2 options/value (tags) to the controller in asp.net mvc? 在MVC Controller的下拉列表中设置选定的值 - Set selected value in dropdownlist from MVC Controller MVC视图不要在下拉列表中选择空值 - MVC View do not select null value in dropdownlist 如何在控制器(MVC)中获取 DropDownList 的选定值 - How to get the selected value of a DropDownList in controller (MVC) 如何将下拉列表的值获取到MVC中的控制器 - how to get the value of a dropdownlist to the controller in MVC 如何将 DropDownList 中的选项值返回到 ASP.NET 中的 Controller - How do I return option value from DropDownList into Controller in ASP.NET 如何获得MVC4中第二个下拉列表的值? - How do I get the value of my second dropdownlist in MVC4? 如何在MVC中获取DropDownList的选定值 - How do I get the selected value of a DropDownList in MVC 如何从控制器中的HTML.DropDownList获取HTML - How do I get the HTML from an HTML.DropDownList in the controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM