0

I am creating a registration form, there is a field called gender in drop down list. when I select Male or Female from the gender drop down list the value will always be null and return a validation message "Male or Female is not valid for the field"

controller

 [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "ID, FirstName,DOB,Gender")] AgentMaster agentMaster)
    {
        if (string.IsNullOrEmpty(Convert.ToString(agentMaster.Gender)))
        {
            ModelState.AddModelError("Gender", "Gender is required");
        }


        if (ModelState.IsValid)
        {
            db.AgentMasters.Add(agentMaster);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        return View(agentMaster);
    }

view

@model A.Models.AgentMaster

 <div class="form-group">
        @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">

            @Html.DropDownListFor(model => model.Gender, new SelectList(new[] { "Male", "Female" }),"--select--")
            @Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
        </div>
    </div>

in model gender is given as

public Nullable<short> Gender { get; set; }

when I debug the code using break points the vale for gender in [httppost] is null, but by giving 1 or 0 it works. so I use validation in controller to convert to string, but it is not working. Iam not getting where I need to change. can anyone please help to find solution ??

1 Answer 1

0

Your generating a <select> which posts back a string (either "Male" or "Female" which cannot be bound to a short.

Change the property to string, or if you do want short then you need to generate an IEnumerable<SelectListItem> that has the corresponding Value, for example

new List<SelectListItem>()
{
    new SelectListItem() { Value = "0", Text = "Male" },
    new SelectListItem() { Value = "1", Text = "Female" }
};

however, storing a Gender as a numeric value makes no sense, and the property should be a string or an enum.

Sign up to request clarification or add additional context in comments.

8 Comments

I have got this as correct but the value is not showing in the details , delete and index page ??
I have no idea what your code in those methods are (and I don't even know which of the above options you chose) I assume you chose the correct one which is a string so not sure what your problem is.
I have written by using text and value, the value 0 or 1 is entering to the database but is showing in the index page (index action method)
Showing what in the index page? - if you really have kept it as 0 and 1, then of course it will shown in the Index page a 0 and 1! What are you expecting?
gender value is blank in the index page but it is showing in the database. the data is not retrieving from table
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.