1

Values of all fields set to Action correctly except selected value of Gender dropdown list.

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

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

This is issue

 @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"})

It always NULL in Action. I tried Html.DropDownList("Gender".... - it's work, I get selected Value of Gender but stop working Required validation.

I Model

 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)
     {......}

I also tried Bind in Action

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

And it's work. I get value of selected Gender but stop working all other - all fields null except Gender.

What I'm doing wrong?

Leonardo Henriques
  • 784
  • 1
  • 7
  • 22
ewqewqewqe
  • 51
  • 2
  • 9
  • You cannot use a `Tuple` in a view that generates form controls (for a strart does not have a parameter-less constructor so it cannot be bound in the POST method). And you creating a dropdownlist with a name that has no relationship at all to you model. Use a view model containing the properties you want and bind to then using the strongly types `**For()` methods –  Mar 01 '18 at 10:01
  • Don't understand. My Dropdown have name Gender and I have Gender string in my model - RegisterNewPatient (Item3.Gender) – ewqewqewqe Mar 01 '18 at 10:51
  • Create a view model containing all the properties you need in the view - [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc). And refer the code [this Q/A](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) to create a dropdownlist –  Mar 01 '18 at 10:53
  • I already have model - RegisterNewPatient. It contains not only Gender. it contains all fields that I paste within @using (Html.BeginForm("CreateNewPatient".... – ewqewqewqe Mar 01 '18 at 11:02
  • No, you have a `Tuple`! Do not use a `Tuple`. It is not clear why you have a `Tuple` and what the other items are for, but if you need to display properties from other models, put them in your view model as well. –  Mar 01 '18 at 11:04
  • Stephen, yep, you're right. Tuple - bad thing. Thank's! – ewqewqewqe Mar 01 '18 at 11:42

0 Answers0