如何从Html中获取值.ListBox在asp.net MVC

本文关键字:ListBox asp net MVC 获取 Html | 更新日期: 2023-09-27 18:04:26

我之前有过这个

<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>

,在我的控制器上,我可以获得值:(检查请求。轮廓线)

      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,因为我需要它是多重选择,现在我没有看到它在请求。形式收集

如何获得选中的值?

如何从Html中获取值.ListBox在asp.net MVC

像这里一样使用FormCollection

一样:

public ActionResult MyAction(FormCollection formCollection)
{
    var addedItems = formCollection["Empresa"].Split(',');
    //....more code that does stuff with the items
}