I want to be able to select a row in a table (which represents an invoice), choose a radio button which has an amount value and press ‘Confirm’. Once confirm is clicked, I want the status of the selected invoice to change to “Paid” or “Partly Paid” depending on the payment amount select. I also want a new payment entry to be added to the ClientPayments table that is related to the selected invoice, I want the PaymentAmount to be the radio button amount and the date to be the system date.
The code I have at the moment is producing an error, namely: The parameters dictionary contains a null entry for parameter ‘id’ of non-nullable type ‘System.Int32’.
Here is the code I have so far. In the View:
@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' data-id=@item.InvoiceNumberID >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>
Confirm Payment
And the code in the controller is:
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();
}
The javascript alert shows the right ID that I select, but when I click Confirm, it doesn’t find the ID. Can anyone see where I’m going wrong?
Thanks,
Amy
1) For the postback to work, you need to submit your form properly, for instance have a submit button inside your form;
2) For adding a new entry to the ClientPayments table, you do something along the line of