ASP.NET MVC。元组模型 - DropdownFor始终将null设置为Action

时间:2018-03-01 09:57:34

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

除“性别”下拉列表的选定值外,所有字段的值都设置为“正确操作”。

[HttpGet]
    public ActionResult AddUser(int roleId)
    {    .....
    List<Gender> genderList = new List<Gender>
            {
                new Gender {Name = "Male", Value = "M"},
                new Gender {Name = "Female", Value = "F"}
            };

            ViewBag.Genders = genderList;
        return View();
    }

查看

@model Tuple<UserSearch, UserCreateBindModel, RegisterNewPatient>
@using (Html.BeginForm("CreateNewPatient", "Companies", new { @class="form-inline", Area = "" }, FormMethod.Post))
                        {
                        Html.EnableClientValidation();
                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                        @Html.AntiForgeryToken()

 @Html.LabelFor(m => m.Item3.FirstName)
                                    </div>
                                    @Html.TextBoxFor(m => m.Item3.FirstName, new { Name = "FirstName", @class = "form-control input-lg", @id="firstName", placeholder = "First Name" })


 @Html.LabelFor(m => m.Item3.LastName)
                                    </div>
                                @Html.TextBoxFor(m => m.Item3.LastName, new { Name = "LastName", @class = "form-control input-lg", @id = "lastName", placeholder = "Last Name" })

 <div class="">@Html.LabelFor(m => m.Item3.State)</div>
                                    @Html.DropDownList(
                                    "State",
                                    new SelectList(ViewBag.AllStates,
                                    "Id",
                                    "Name", 0),
                                    "",
                                    new {@class = "form-control input-lg dropdown", style= "width:35%;" })
                                </div>

// and many others fields

这是问题

 @Html.LabelFor(m => m.Item3.Gender)
                        </div>
                        @Html.DropDownListFor(m => m.Item3.Gender, new SelectList(ViewBag.Genders,
                    "Value",
                    "Name", userData.Gender),
                    "",
                    new { @class = "form-control input-lg" })
                     @Html.ValidationMessageFor(m => m.Item3.Gender, 
                                "", new {@class = "text-danger"})

Action中始终为NULL。 我尝试了Html.DropDownList(“性别”.... - 它的工作,我选择了性别的价值,但停止工作所需的验证。

我  的模型

 public class RegisterNewPatient
{ .......
  [Required(ErrorMessage = "The Gender is required")]
    public string Gender { get; set; }
}
public class Gender
{
    public string Value { get; set; }
    public string Name { get; set; }
}

    [HttpPost]
    public async Task<ActionResult> CreateNewPatient(RegisterNewPatient patient)
     {......}

我也尝试过Bind in Action

CreateNewPatient([Bind(Prefix="Item3")]RegisterNewPatient patient)

这是有效的。我获得了所选性别的价值,但停止了所有其他 - 所有字段为null,除了性别。

我做错了什么?

0 个答案:

没有答案