1

I had this before

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

and on my controller I could get the values: (check the request.form line)

      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();
        }
    }

However I changed DropDownList with ListBox because I need it to be multipleselect, and now I dont see it in the Request.Form collection

How can I get the values selected?

Luis Valencia
  • 32,619
  • 93
  • 286
  • 506
  • This should help you: http://stackoverflow.com/questions/1255472/how-does-a-multiple-select-list-work-with-model-binding-in-asp-net-mvc – Fals Jul 23 '15 at 22:00
  • Using `Request.Form[]` in MVC? A ` –  Jul 23 '15 at 22:00
  • This question I asked before will explain why I need it that way: http://stackoverflow.com/questions/31575079/add-a-dropdown-for-a-field-not-in-the-model-asp-net-mvc – Luis Valencia Jul 23 '15 at 22:02
  • basically the model is a class which I dont control its in the ADAL library, and I need an extra COMBOBOX so users can select one or multiple companies, then on the controller I can save in schema extension which companies did the user selected. – Luis Valencia Jul 23 '15 at 22:04
  • This may be your best bet http://stackoverflow.com/a/3743890/376550 – drooksy Jul 23 '15 at 22:10
  • Then as I noted in that question, use a view model and map your data class to and from a view model or add a parameter to your method - `int[] Empresas` –  Jul 23 '15 at 22:13

1 Answers1

1

Use FormCollection like they have it here.

Like so:

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

    //....more code that does stuff with the items
}
Community
  • 1
  • 1