【问题标题】:How to pass value selected from DropDownList to Controller - MVC如何将从 DropDownList 中选择的值传递给控制器​​ - MVC
【发布时间】:2020-10-22 15:48:55
【问题描述】:

我无法在 Homecontroller 的下拉列表中选择值。我有一个表单中的 DropDownList,但我认为我的格式可能是错误的。我是 MVC 和 HTML 的新手,所以我很努力。不胜感激。

这是我的控制器(我把它放在我的家庭控制器中,这是个坏主意吗?):

public IActionResult Index()
{
    _ = new List<MyjsonSettings>();
    var obj = new StatusPortController(configuration);
    List<MyjsonSettings> PortList = obj.GetPortNum();
    List<SelectListItem> AppNameList = PopulateDropDown(PortList);
    
    ViewData["Applications"] = AppNameList;
    
    return View("~/Views/Home/dataview.cshtml");
}
    
public List<SelectListItem> PopulateDropDown(List<MyjsonSettings> PortList)
{
    List<SelectListItem> AppNameList = new List<SelectListItem>();
    
    for (int i = 0; i < PortList.Count(); i++)
    {
        AppNameList.Add(new SelectListItem {
            Text = PortList[i].NAME, Value = (i+1).ToString()
        });
    }
    
    return AppNameList;
}

这是视图(dataview.cshtml):

@{
   ViewData["Title"] = "Home Page";
}


@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    @Html.DropDownList("Applications", ViewData["AppNameList"] as List<SelectListItem>)

    <input type="submit" value="submit" />
}

有什么想法吗?运行时没有错误,只是不知道如何获取响应。

【问题讨论】:

    标签: html asp.net-mvc model-view-controller drop-down-menu view


    【解决方案1】:

    您可以将结构重建为更有用的方式,并且为了提交带有下拉列表或任何类型字段的表单,您需要首先返回带有模型的视图,然后将表单提交给接收相同模型的操作类型作为参数

    示例:

    型号:

    public class ApplicationsAddModel {
         public ApplicationsAddModel (){
               //constructer to initialize the list 
               ApplicationsList  = new List<SelectListItem>();
         }
     
         public string test{ get; set; }     
         public int selectedApplicationId { get; set; }         
         public List<SelectListItem> ApplicationsList { get; set; } 
    }
    

    控制器

    //this is the first action that return the model 
    [HttpGet]
    public IActionResult Index()
    {
        ApplicationsAddModel model = new ApplicationsAddModel (); 
        //fill your drop down list
        List<SelectListItem> AppNameList = PopulateDropDown(PortList);
        model.ApplicationsList = AppNameList;
        return View(model);
    }
    
    [HttpPost] //recive the form
    public IActionResult Index(ApplicationsAddModel SubmittedModel)
    {
        var selectedApplication = SubmittedModel.selectedApplicationId; //get the selected value from ddl
    
        //fill your drop down list
        List<SelectListItem> AppNameList = PopulateDropDown(PortList);
        model.ApplicationsList = AppNameList;
        return View(SubmittedModel);
    }
    

    查看(index.cshtml):

    @model projectName.ApplicationsAddModel 
    @{ ViewData["Title"] = "Home Page"; }
    
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {  
          @Html.LabelFor(m => m.selectedApplicationId)
          @Html.DropDownListFor(m => m.selectedApplicationId, Model.ApplicationsList, "---", new { @class = "custom-select form-control" }) 
          <input type="submit" value="submit" />
    }
    

    总结: 在 MVC 中,当您必须向控制器提交数据时,创建模型,转到控制器并创建您的第一个操作 (GET),该操作用初始数据填充表单并填写您的下拉列表(如果存在),然后创建 (POST) 操作接收到同类型视图的模型,MVC会自动为你绑定

    最好的问候

    【讨论】:

    • 请问您为什么要在 Httpost Index() 中再次填写下拉列表?
    • @anasteele 如果您尝试评论或删除这部分代码,那么在发布表单后下拉列表将在视图中为空,因此为了保持它充满我们必须的项目提交表单后重新填写(提交后返回模型查看)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    相关资源
    最近更新 更多