【问题标题】:How to get the values from Html.ListBox in asp.net MVC如何从 asp.net MVC 中的 Html.ListBox 获取值
【发布时间】:2015-10-14 10:19:57
【问题描述】:

我以前有过这个

<div class="form-group">
    @Html.Label("Empresa", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
       @Html.DropDownList("Empresa", 
         new SelectList(empresas, "Id", "Nombre"), 
         new { @class = "form-control" })
     </div>
</div>

在我的控制器上我可以得到值:(检查 request.form 行)

      public async Task<ActionResult> Create(
        [Bind(
            Include =
                "UserPrincipalName,AccountEnabled,PasswordProfile,MailNickname,DisplayName,GivenName,Surname,JobTitle,Department"
            )] Microsoft.Azure.ActiveDirectory.GraphClient.User user)
    {
        ActiveDirectoryClient client = null;
        try
        {
            client = AuthenticationHelper.GetActiveDirectoryClient();
        }
        catch (Exception e)
        {
            if (Request.QueryString["reauth"] == "True")
            {
                //
                // Send an OpenID Connect sign-in request to get a new set of tokens.
                // If the user still has a valid session with Azure AD, they will not be prompted for their credentials.
                // The OpenID Connect middleware will return to this controller after the sign-in response has been handled.
                //
                HttpContext.GetOwinContext()
                    .Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
            }

            //
            // The user needs to re-authorize.  Show them a message to that effect.
            //
            ViewBag.ErrorMessage = "AuthorizationRequired";
            return View();
        }

        try
        {
            var usuario = user.UserPrincipalName;
            user.UserPrincipalName = usuario+SettingsHelper.Domain;
            user.MailNickname = usuario;
            user.AccountEnabled = true;
            await client.Users.AddUserAsync(user);

            string extPropLookupName = string.Format("extension_{0}_{1}", SettingsHelper.ClientId.Replace("-", ""), "Compania");

            //TO BE FINISHED
            user.SetExtendedProperty(extPropLookupName, Request.Form["Empresa"].ToString());
            await user.UpdateAsync();
            //Task.WaitAll();

            // Save the extended property value to Azure AD.
            user.GetContext().SaveChanges();
            return RedirectToAction("Index");
        }
        catch (Exception exception)
        {
            ModelState.AddModelError("", exception.Message);
            return View();
        }
    }

但是我用 ListBox 更改了 DropDownList,因为我需要它是多选的,现在我在 Request.Form 集合中看不到它

如何获得选定的值?

【问题讨论】:

  • 这应该对你有帮助:stackoverflow.com/questions/1255472/…
  • 在 MVC 中使用 Request.Form[]&lt;select multiple&gt; 回发一组值,因此它将绑定到 int[]string[] 的属性。
  • 我之前提出的这个问题将解释为什么我需要它:stackoverflow.com/questions/31575079/…
  • 基本上模型是一个类,我不能在 ADAL 库中控制它,我需要一个额外的 COMBOBOX 以便用户可以选择一个或多个公司,然后在控制器上我可以保存在模式扩展中用户选择的公司。
  • 这可能是你最好的选择stackoverflow.com/a/3743890/376550

标签: c# asp.net asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

像他们拥有的那样使用 FormCollection here

像这样:

public ActionResult MyAction(FormCollection formCollection)
{
    var addedItems = formCollection["Empresa"].Split(',');

    //....more code that does stuff with the items
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多