简体   繁体   English

Asp.Net MVC:如何在提交时从 DropDownListFor 中获取选定的选项

[英]Asp.Net MVC:How to get selected option from DropDownListFor in Action on submit

I have dropdownlist in Asp.net form and after selected option on submit doesn't getting value in Action.我在 Asp.net 表单中有下拉列表,并且在选择提交选项后没有在 Action 中获得价值。

Not getting what im missing and selected options not retrieving in Action .Here is the code没有得到我缺少的东西,并且选择的选项没有在 Action 中检索。这是代码

ViewPage视图页面

<div class="inner-box">

        @using ICommonInterfaces.Model
        @model LoginModel
        @using (Html.BeginForm("ApplicationSwitcher", "Home", FormMethod.Get))
        {
          @Html.DropDownListFor(m => m.ApplicationNames, new SelectList(Model.ApplicationNames, "TenantId", "ApplicationId"), "Select Application",
       new { @class = "textboxStyle", placeholder = "Select your application...", autofocus = "autofocus",
         autocomplete = "on", onkeypress = "btnFocusActivate(this, event)", id = "un" })
          <input type="submit" value="NEXT" class="rectangle" id="submitNext" disabled="disabled" />
        }       
</div>

Model模型

namespace ICommonInterfaces.Model
{
    public class LoginModel
    {
        public List<TenantApplicationNames> ApplicationNames { get; set; }
    }

    public class TenantApplicationNames
    {
        public string TenantId { get; set; }
        public string ApplicationId { get; set; }
    }
}

On Submit HomeController Action提交 HomeController 动作

    [HttpGet]
    public async Task<IActionResult> ApplicationSwitcher(LoginModel loginModel)
    {
      var applicationNames = loginModel.ApplicationNames; // Count zero
      Console.WriteLine(applicationNames.Count)//0
    }

what is the reason not getting selected options?How to get it?没有得到选择的选项是什么原因?如何得到它?

Thanks.谢谢。

I'd change the method to POST and get the value of selected item using Request.Form["nameOfYourDDL"]我将方法更改为 POST 并使用 Request.Form["nameOfYourDDL"] 获取所选项目的值

Like:喜欢:

<div class="inner-box">

    @using ICommonInterfaces.Model
    @model LoginModel
    @using (Html.BeginForm("ApplicationSwitcher", "Home", FormMethod.Post))
    {
      @Html.DropDownListFor("ddlTest", m => m.ApplicationNames, new SelectList("ddlTeste", Model.ApplicationNames, "TenantId", "ApplicationId"), "Select Application",
   new { @class = "textboxStyle", placeholder = "Select your application...", autofocus = "autofocus",
     autocomplete = "on", onkeypress = "btnFocusActivate(this, event)", id = "un" })
      <input type="submit" value="NEXT" class="rectangle" id="submitNext" disabled="disabled" />
    }   

And in the controller:在控制器中:

    [HttpPost]
public async Task<IActionResult> ApplicationSwitcher(LoginModel loginModel)
{
     var selectedValue = Request.Form["ddlTest"].ToString
}

I test the form that is generated by @using (Html.BeginForm() . In controller, loginModel receives data with the type of array. In order to get the drop-down list's value in the controller, this code need to be modified.我测试了@using (Html.BeginForm()生成的@using (Html.BeginForm() 。在控制器中, loginModel接收数组类型的数据。为了在控制器中获取下拉列表的值,需要修改此代码。

In ViewPage, m.ApplicationNames need to be changed to array.在 ViewPage 中,需要将m.ApplicationNames更改为数组。 m.ApplicationNames[0].TenantId

<div class="inner-box">

    @using ICommonInterfaces.Model
    @model LoginModel
    @using (Html.BeginForm("ApplicationSwitcher", "Home", FormMethod.Get))
    {
        @Html.DropDownListFor(m => m.ApplicationNames[0].TenantId, new SelectList(Model.ApplicationNames, "TenantId", "ApplicationId"), "Select Application",
     new
     {
         @class = "textboxStyle",
         placeholder = "Select your application...",
         autofocus = "autofocus",
         autocomplete = "on",
         onkeypress = "btnFocusActivate(this, event)",
         id = "un",
     })
        <input type="submit" value="NEXT" class="rectangle" 
         id="submitNext" @*disabled="disabled"*@ />
    }
</div>

Result.结果。 在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM