我在我的MVC 4站點上有一個儀表板,它部分顯示了一個數據庫表(我的數據庫是使用實體框架代碼優先方法生成的)中的數據列表。正在顯示的數據是用於訂單列表以及與該訂單一致的信息。現在,我正在嘗試添加爲同一訂單添加額外數量的功能(例如:我需要此訂單報價爲10,25,50和100件)。在每行的編輯,詳細信息和刪除選項旁邊都有一個添加按鈕,用戶可以在該按鈕上添加另一個數量。但是,在測試中,當我提交數量時,出現驗證錯誤時表示輸入的值在應該是完全有效的輸入時無效。有人可以幫助,因爲我無法弄清楚爲什麼會發生這種情況。下面是一些相關的代碼:在MVC 4有效輸入上引發驗證錯誤應用程序
型號:
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; }
}
儀表板控制器:
[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);
}
查看 個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>
}
正如你所看到的,在我的模型數量是一個整數,如果我輸入一個整數到我的形式拋出驗證錯誤。
在此先感謝任何可以幫助的人,我很感激。
凡被拋出的錯誤?你到達「ModelState.IsValid」行嗎? – CorrugatedAir 2013-04-10 18:19:38
我也不明白你的問題... – 2013-04-10 18:52:03