2011-09-05 45 views
0

我希望能夠選擇在表格中的行(代表發票),選擇它有一個量的值,然後按「確認」單選按鈕。點擊確認後,我希望所選發票的狀態根據付款金額選擇更改爲「付費」或「部分付款」。我還希望將新的付款條目添加到與所選發票相關的ClientPayments表中,我希望PaymentAmount是單選按鈕數量,日期是系統日期。更新所選行實體框架MVC 3

我目前的代碼生成錯誤,即:參數字典包含非可空類型「System.Int32」參數「ID」空項。

這裏是我到目前爲止的代碼。在查看:

@using (Html.BeginForm("Confirm", "InvoiceController")) 
{ 

foreach (var item in Model) 
{ 
string selectedRow = ""; 
if (item.InvoiceNumberID == ViewBag.InvoiceNumberID) 
{ 
    selectedRow = "selectedRow"; 
} 

<tr class="@selectedRow" valign="top"> 
      <td> 
      <a href='javascript:void(0)' class='select' [email protected]  >Select</a> 


     </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.InvoiceNumberID) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.InvoiceAmount) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.InvoiceMonth) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.InvoiceStatus) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.Client.FullName) 
    </td> 
    <td> 
     @Html.ActionLink("Edit", "Edit", new { id = item.InvoiceNumberID }) | 
     @Html.ActionLink("Details", "Details", new { id = item.InvoiceNumberID }) | 
     @Html.ActionLink("Delete", "Delete", new { id = item.InvoiceNumberID }) 
    </td> 
</tr> 
} 
<input type='hidden' id='id' name='id' value='0' /> 
} 
</table> 
<br /> 

<p><i>Select or type in a custom amount to confirm as paid:</i> 
</p> 
<table> 
<tr><td><b>Monthly Amounts:</b></td><td><b>Weekly Amounts:</b></td></tr> 
<tr><td>Private Lesson (1 Hour) @Html.RadioButton("InvoiceAmount", "640", true) R640.00<br /></td><td>Private Lesson (1 Hour) @Html.RadioButton("InvoiceAmount", "160", true) R160.00<br /> </td></tr> 
<tr><td>Private Lesson (1/2 Hour) @Html.RadioButton("InvoiceAmount", "350", true) R350.00<br /></td><td>Private Lesson (1 Hour) @Html.RadioButton("InvoiceAmount", "87.50", true) R87.50<br /></td></tr> 
<tr><td>Group Lesson (1 Hour) @Html.RadioButton("InvoiceAmount", "460", true) R460.00</td> <td>Private Lesson (1 Hour) @Html.RadioButton("InvoiceAmount", "115", true) R115.00<br /></td> </tr> 
<tr><td>Custom Amount @Html.RadioButton("InvoiceAmount", "115", true) @Html.TextBox("customAmount")<br /></td></tr> 
</table> 

<script type='text/javascript'> 
    $('.select').click(function(){ 
      $('#id').val($(this).attr('data-id')); 
       alert($('#id').val()); 
      }); 
</script> 


確認付款

並在控制器中的代碼是:

public ActionResult Confirm(int id, long InvoiceAmount) 
    { 
     Invoices invoices = db.Invoice.Find(id); 
     //now validate that if the logged in user is authorized to select and confirm this invoice or not. 


     if (InvoiceAmount != invoices.InvoiceAmount) 
     { 
      invoices.InvoiceStatus = "Partly Paid"; 

     } 
     else 
     { 
      invoices.InvoiceStatus = "Confirmed"; 
     } 
     db.Entry(invoices).State = EntityState.Modified; 
     db.SaveChanges(); 

     return View(); 
    } 

JavaScript的警告表明,我選擇了正確的ID,但是當我點擊確認,它沒有找到該ID。任何人都可以看到我要去哪裏嗎?

感謝, 艾米

+0

行做一些事情是什麼元素看起來像一個包含ID屬性你$(「#ID」)中進行選擇? –

+0

該元素應該是通過按'選擇'鏈接選擇的任何行。像​​Select Amy

+0

所以'id'必須返回所選行的InvoiceNumberID – Amy

回答

0

1)回發的工作,你需要正確提交您的形式,例如有一個提交表單內按鈕;
2)添加新條目ClientPayments表,你沿着

if (InvoiceAmount != invoices.InvoiceAmount) 
{ 
    invoices.InvoiceStatus = "Partly Paid"; 
} 
else 
{ 
    invoices.InvoiceStatus = "Confirmed"; 
} 
db.Entry(invoices).State = EntityState.Modified; 

var clientPayment = new ClientPayment(); 
// Set clientPayment properties 
db.ClientPayments.Add(clientPayment); 

db.SaveChanges(); 
+0

非常感謝!還有一個問題 - 我如何將PaymentDate設置爲當前系統日期?是否有一些簡單的方法來說clientPayment.PaymentDate = *排序疼痛的現在的日期*? – Amy

+0

[這裏](http://www.google.se/search?q = C%23 +當前日期)您可以找到更多信息 –

+0

我的意思是clientPayments.PaymentDate = systemdate?我沒有意識到asterix製作的奇怪格式 – Amy