wrigley06
wrigley06

Reputation: 359

Validation error being thrown on valid input in MVC 4 App

I have a dashboard on my MVC 4 site that in part displays a list of data from one db table (my database was generated using the Entity Framework code-first method). The data being displayed is for a list of orders and the information that goes along with that order. Now, I'm trying to add the ability to add additional quantities for the same order (ex: I need this order quoted for 10, 25, 50 and 100 pieces). Right next to the edit, details, and delete options on each row is an add button where the user would then go to add another quantity. However, in testing, when I go to submit a quantity, the validation error comes up saying that the value entered is invalid when it should be a completely valid input. Can someone please help, because I can't figure out why this is happening. Here's some of the relevant code:

Models:

General_Info.cs

public class General_Info
{
    [Key]
    public int Quote_ID { get; set; }

    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime Open_Quote { get; set; }
    public string Customer_Name { get; set; }
    public string OEM_Name { get; set; }
    public int Qty { get; set; }
    public string Fab_Drawing_Num { get; set; }
    public string Rfq_Num { get; set; }
    public string Rev_Num { get; set; }
    public string Group_Number { get; set; }

    public General_Info()
    {
        Quantitys = new HashSet<Quantity>();
        Quote_Datas = new HashSet<Quote_Data>();
    }

    public virtual ICollection<Quantity> quantitys { get; set; }
}

Quantity.cs

public class Quantity
{
    [Key]
    public int Qty_ID { get; set; }

    public string Group_Number { get; set; } // foreign key
    public int quantity { get; set; }
    public General_Info General_Info { get; set; }

}

Dashboard Controller:

[HttpGet]
    public ActionResult AddQuantity(int Group_Num = 0)
    {
        return View();
    }

    [HttpPost]
    public ActionResult AddQuantity(Quantity quantity)
    {
        if (ModelState.IsValid)
        {
            db.Quantitys.Add(quantity);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(quantity);
    }

View AddQuantity.cshtml

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Quantity</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.quantity)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.quantity)
        @Html.ValidationMessageFor(model => model.quantity)
    </div>
    <p>
        <input type="submit" value="Add Qty" />
    </p>
</fieldset>
}

As you can see, in my model quantity is an integer, and if I enter an integer into my form the validation error is thrown.

Thanks in advance to anyone who can help, I appreciate it.

Upvotes: 0

Views: 295

Answers (1)

maxlego
maxlego

Reputation: 4914

if you change your binding model name, that should do it

[HttpPost]
public ActionResult AddQuantity(Quantity model)

Upvotes: 1

Related Questions