asp.netasp.net-mvcasp.net-mvc-2asp.net-mvc-2-validation

Server Side DropDown validation firing always


My MVC 2 dropdown validation fire always even though i select a item from the list. I checked from firebug the dropdown is bind correcly with expected values.

Model:

public class Delivery
{
    public int DeliveryID { get; set; }

   [Required(ErrorMessage = "Please Select a Stock")]
    public int? StockID { get; set; }


     [Required(ErrorMessage = "Please Enter Expenses")]
    public double OtherExpenses { get; set; }

    public double Total { get; set; }

        [Required(ErrorMessage = "Please Enter Description")]
    public string Description { get; set; }

        [Required(ErrorMessage = "Please Enter Arrived Date")]
    public DateTime ArrivedDate { get; set; }

    public string StockDescription { get; set; }

    public IEnumerable<Stock> lstStock { get; set; }
}

Controller:

public ActionResult Create()
        {
            DeliveryRepository rep = new DeliveryRepository();
            var model = new Delivery
            {
                lstStock = rep.GetStock()
            };
            return View(model);
        } 

        //
        // POST: /Delivery/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {

                    Delivery del = new Delivery();
                    DeliveryRepository rep = new DeliveryRepository();
                    TryUpdateModel(del);
                    if (ModelState.IsValid)
                    {// TODO: Add insert logic here

                        del.ArrivedDate = Convert.ToDateTime(Request.Form["ArrivedDate"]);
                        del.Description = Request.Form["Description"];
                        del.OtherExpenses = Convert.ToDouble(Request.Form["OtherExpenses"]);
                        del.StockID = Convert.ToInt32(Request.Form["StockID"]);
                        del.Total = Convert.ToDouble(Request.Form["lblTotal"]) + del.OtherExpenses;
                        rep.Create(del);
                        rep.Save();
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        var model = new Delivery
                        {
                            lstStock = rep.GetStock()
                        };
                        return View(model);
                    }
            }
            catch
            {
                return View();
            }
        }

View:

 <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Fields</legend>

             <div class="editor-label">
                <%: Html.Label("Select Stock") %>
            </div>
            <div class="editor-field">
               <%: Html.DropDownListFor(x => x.lstStock, new SelectList(Model.lstStock, "StockID", "Description"), "-- Please Select a Stock --")%>
                 <%: Html.ValidationMessageFor(model => model.StockID)%>
            </div>

            <div id="clslbl">
            <br />
                <label id="lblTotal"></label>

            </div>
           <br />
            <div class="editor-label">
                <%: Html.LabelFor(model => model.OtherExpenses) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.OtherExpenses) %>
                <%: Html.ValidationMessageFor(model => model.OtherExpenses) %>
            </div>


            <div class="editor-label">
                <%: Html.LabelFor(model => model.Description) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Description) %>
                <%: Html.ValidationMessageFor(model => model.Description) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.ArrivedDate) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.ArrivedDate) %>
                <%: Html.ValidationMessageFor(model => model.ArrivedDate) %>
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

    <% } %>

Solution

  • Issue was with my view

     <div class="editor-field">
                   <%: Html.DropDownListFor(x => x.lstStock, new SelectList(Model.lstStock, "StockID", "Description"), "-- Please Select a Stock --")%>
                     <%: Html.ValidationMessageFor(model => model.StockID)%>
                </div>
    

    Should be changed as

     <div class="editor-field">
                   <%: Html.DropDownListFor(x => x.StockID, new SelectList(Model.lstStock, "StockID", "Description"), "-- Please Select a Stock --")%>
                     <%: Html.ValidationMessageFor(model => model.StockID)%>
                </div>