视图模型中的SelectListItem属性不会从视图传递回控制器

时间:2017-07-04 12:25:19

标签: asp.net-mvc model-view-controller asp.net-mvc-viewmodel

我有这个视图模型:

public class UserViewModel
{
    public string ID { get; set; }

    [Required]
    [Display(Name = "Nombre")]
    public string Nombre { get; set; }

    [Required]
    [Display(Name = "Login")]
    public string Login { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Contraseña")]
    public string Password { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "E-mail")]
    public string Email { get; set; }

    [DataType(DataType.PhoneNumber)]
    [Display(Name = "Teléfono")]
    public string PhoneNumber { get; set; }

    [Display(Name = "Ciudad")]
    public System.Web.Mvc.SelectListItem City { get; set; }

    [Display(Name = "Empresas")]
    public IEnumerable<System.Web.Mvc.SelectListItem> Empresas { get; set; }

    [Required]
    [Display(Name = "Perfil")]
    public string Role { get; set; }

    [Display(Name = "Está Vigente")]
    public bool Vigente { get; set; }
}

我遇到的问题是,当我在City和Empresas中选择表单中的内容时(它们都是SELECT字段),它们不会在UserViewModel参数中传递给控制器​​:

    public JsonResult EditUser(UserViewModel model)
    {
          .....
    }

仅传递其他属性。

管理此问题的正确方法是什么?

编辑:

这是观点的一部分:

            @using (Html.BeginForm("EditUser", "Account", new { area = "Security" }, FormMethod.Post, new { @class = "form-horizontal form-label-left", role = "form", novalidate = "novalidate", id = "frmUsuario" }))
            {
                var id = Model == null ? String.Empty : Model.ID;
                <div class="errores col-md-12 col-sm-12 col-xs-12" style="display: none">
                    <div class='validation-summary-errors alert alert-danger alert-white rounded' data-valmsg-summary="true">
                        <div class="icon">
                            <i class="fa fa-times-circle"></i>
                        </div>
                        <p>Debe completar los campos marcados en forma correcta.</p>
                    </div>
                </div>
                @Html.AntiForgeryToken();
                <input type="hidden" value="@id" id="id" name="id" />
                <div class="item col-md-12 col-sm-12 col-xs-12 form-group has-feedback">
                    @Html.TextBoxFor(m => m.Nombre, new { @class = "form-control has-feedback-left", placeholder = "Nombre", required = "required" })
                    <span class="fa fa-user form-control-feedback left" aria-hidden="true"></span>
                </div>
                <div class="item col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
                    @Html.TextBoxFor(m => m.Login, new { @class = "form-control has-feedback-left", placeholder = "Login", required = "required" })
                    <span class="fa fa-sign-in form-control-feedback left" aria-hidden="true"></span>
                </div>

                <div class="item col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
                    @{
                        //var requerido = String.IsNullOrEmpty(userId) ? "required" : String.Empty;
                        var requerido = Model == null || String.IsNullOrEmpty(Model.ID);
                        object htmlAttributes = null;
                        if (requerido)
                        {
                            htmlAttributes = new { @class = "form-control has-feedback-left", placeholder = "Contraseña", required = "required" };
                        }
                        else
                        {
                            htmlAttributes = new { @class = "form-control has-feedback-left", placeholder = "Contraseña" };
                        }
                    }
                    <!-- Para evitar que los password managers llenen los campos -->
                    <input type="text" id="hidUser" style="display: none" />
                    <!-- Para evitar que los password managers llenen los campos -->
                    <input type="password" id="hidPassword" style="display: none" />

                    @Html.PasswordFor(m => m.Password, htmlAttributes)
                    <span class="fa fa-key form-control-feedback left" aria-hidden="true"></span>
                </div>

                <div class="item col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
                    @Html.TextBoxFor(m => m.Email, new { @class = "form-control has-feedback-left", placeholder = "E-mail", required = "required" })
                    <span class="fa fa-envelope form-control-feedback left" aria-hidden="true"></span>
                </div>

                <div class="item col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
                    @Html.TextBoxFor(m => m.PhoneNumber, new { @class = "form-control has-feedback-left", placeholder = "Teléfono" })
                    <span class="fa fa-phone form-control-feedback left" aria-hidden="true"></span>
                </div>

                <div class="item col-md-12 col-sm-12 col-xs-12 form-group">
                    @Html.DropDownListFor(m => m.City, new SelectList(new List<SelectListItem>()), new { style = "width: 100%" })
                </div>

                <div class="item col-md-12 col-sm-12 col-xs-12 form-group">
                    @Html.DropDownListFor(m => m.Role, new SelectList(new List<SelectListItem>()), new { style = "width: 100%" })
                </div>

                <div class="item col-md-12 col-sm-12 col-xs-12 form-group">
                    @Html.DropDownListFor(m => m.Empresas, new SelectList(new List<SelectListItem>()), new { style = "width: 100%", multiple = "multiple" })
                </div>

                <div class="item col-md-12 col-sm-12 col-xs-12 form-group">
                    Está Vigente
                    @Html.CheckBoxFor(m => m.Vigente, new { @class = "flat" })
                </div>
            }

2 个答案:

答案 0 :(得分:0)

请在UserViewModal中更改City Property的类型

 public string City { get; set; }

然后它会将所选的选项值返回给控制器,例如

<select
 <option value="1">Delhi </option>
 <option value="2">Surat </option>
</select>

如果您选择德里,则会将值1分配给属性

答案 1 :(得分:0)

我想知道你的下拉列表中没有显示任何内容。 在您的视图模型中,它应至少包含两个属性,例如“AvailableCities”和“City”。

public List<SelectListItem> AvailableCities { get;set; }
public string City { get;set; }

AvailableCities包含应在下拉列表中显示的所有城市。城市正在用于获取用户选择的价值。

在您看来,

 @Html.DropDownListFor(m => m.City, Model.AvailableCities, new { @class = "form-control" })

现在,您应该会看到下拉列表,并且可以在您的操作中获得所选值。