我是ASP.NET MVC的新手,我需要你的幫助。我有項目列表,我想同時編輯它們兩個。ASP.NET MVC:提交項目列表時驗證和數據格式化問題
我的問題是當我使用註釋來格式化顯示和編輯數據時,我無法提交表單,因爲輸入數據無效。
我對元數據類使用數據這裏格式代碼:
[DisplayFormat(DataFormatString = "{0:N0}", ApplyFormatInEditMode = true)]
public Nullable<decimal> amount { get; set; }
[DisplayFormat(DataFormatString = "{0:N0}", ApplyFormatInEditMode = true)]
public Nullable<decimal> price { get; set; }
當我創造新的形式,但提交只有一個項目,我沒有任何驗證的消息。 爲什麼提交1個項目並提交多個項目有區別?
這是我的剃刀代碼來生成列表
@model PTT_WEBSITE.Areas.RM.Models.MonthlyInvoice
@using (Html.BeginForm())
{
@Html.ValidationSummary()
<table class="table">
<thead>
<tr>
<th class="no">No</th>
<th>Payment item</th>
<th width="150">Price</th>
<th width="100">Old number</th>
<th width="100">New number</th>
<th width="100">Quantity</th>
<th width="150">Amount</th>
<th width="50"></th>
</tr>
</thead>
<tbody>
@for(int i = 0; i< Model.MonthlyFees.Count; i++) {
<tr id="[email protected]" >
<td>@i</td>
<td>
@Html.DisplayFor(x => x.MonthlyFees[i].name)
@Html.HiddenFor(x=>x.MonthlyFees[i].paymentitemcode)
@Html.HiddenFor(x=>x.MonthlyFees[i].name)
@Html.HiddenFor(x=>x.MonthlyFees[i].isfixed)
@Html.HiddenFor(x=>x.MonthlyFees[i].ismultiple)
</td>
<td class="align-right"> @Html.EditorFor(x => x.MonthlyFees[i].price) </td>
<td class="align-right">@Html.EditorFor(x => x.MonthlyFees[i].fromnumber)</td>
<td class="align-right"> @Html.EditorFor(x => x.MonthlyFees[i].tonumber) </td>
<td class="align-right"> @Html.EditorFor(x => x.MonthlyFees[i].quantity) </td>
<td class="align-right"> @Html.EditorFor(x => x.MonthlyFees[i].amount, new { @ReadOnly = "ReadOnly" }) </td>
<td>
<a href="#" onclick ="deleteitem(@i)"><i class="delete fa fa-trash-o"></i></a>
@Html.HiddenFor(x => x.MonthlyFees[i].isdeleted)
</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td colspan ="6" align="right">Tổng cộng: </td>
<td class="align-right">@String.Format("{0:#,###}",totalAmount)</td>
</tr>
</tfoot>
</table>
}
控制器代碼:
[HttpPost]
public ActionResult Create(MonthlyInvoice invoice)
{
if (ModelState.IsValid)
{
//...
}
//...
return View(invoice);
}
消息:
值'70,000' 是無效的量。
價值'70,000'對價格無效。
'1,700,000'的值對金額無效。
價值'1,700,000'對價格無效。
十進制數不允許使用逗號(,)。我嘗試刪除號碼內的所有逗號並重新提交,它工作正常。 但是,當我在表單上提交它以僅編輯1個項目時,值「1,700,000」被接受,當我使表單提交項目列表時,它不被接受。
我的屏幕 https://www.dropbox.com/s/eagvk4uuu2wabds/problems.PNG?dl=0
任何幫助嗎?
謝謝!
發佈視圖的代碼以及如何生成列表 – 2014-10-18 11:02:54
有點難以理解你在問什麼。您在使用DisplayFormat時無法提交表單,但是在不使用時會起作用?你的'@ Model'看起來像什麼?你是如何創建你的'
你必須做與其他形式不同的事情,如果這是造成問題的原因之一,你應該在你的問題中包含代碼 – 2014-10-19 04:06:06