0
MVC 3プロジェクトでは、テーブルの行を選択していくつかの更新を実行するために非常に奇妙なコードを使用しています。私の問題は、行が選択されているかどうかを検証する方法がわかりません。ここで行が選択されているかどうかを検証するMVC 3
は、私の見解ではコードです:
<p><i>Select an invoice from the grid below:</i></p>
@if (!string.IsNullOrEmpty(TempData["PaymentError"] as string))
{
<div id="error" style="margin: 0 auto; width: 400px;">
<p style="width: 400px;"><img src="../../Content/images/errorsm.png" style="vertical-align: middle; padding: 5px;"/><span style="color: #A62000;font-weight: bold;">@(TempData["PaymentError"] as string)</span></p>
</div>
}
<table id="database">
<tr>
<th></th>
<th>
Invoice Number
</th>
<th>
Invoice Amount
</th>
<th>
Invoice Month
</th>
<th>
Invoice Status
</th>
<th>
Client
</th>
<th></th>
</tr>
@using (Html.BeginForm("Confirm", "Invoice"))
{
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' />
<p>
<a href='@Url.Action("CreateBulkInvoices", "Invoice")'>Generate Invoices</a>
</p>
<table>
<br />
<i>Select an amount below to confirm as paid:</i><br /><br />
<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", "140", 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("InvoiceCustomAmount")<br /></td></tr>
</table>
<br />
<p><i>Select a payment type:</i>
</p>
<p>@Html.RadioButton("PaymentType", "EFT", true) EFT<br />
@Html.RadioButton("PaymentType", "Credit Card", true) Credit Card<br />
@Html.RadioButton("PaymentType", "Cheque", true) Cheque
</p>
<p><input type="submit" value="Confirm" /></p>
}
</table>
<br />
<script type='text/javascript'>
$('.select').click(function(){
$('#id').val($(this).attr('data-id'));
$(this).closest('table').find('tr').removeClass('selectedRow');
$(this).closest('tr').addClass('selectedRow');
});
</script>
そして、私のコントローラのコードは次のとおりです。
public ActionResult Confirm(int id, long InvoiceAmount, string PaymentType, float? InvoiceCustomAmount)
{
var invoice = db.Invoice.Find(id);
//now validate that if the logged in user is authorized to select and confirm this invoice or not.
var clientPayment = new ClientPayments();
clientPayment.InvoiceNumberID = id;
if (InvoiceAmount == 115)
{
InvoiceAmount = (long)InvoiceCustomAmount;
}
var TotalPayments = invoice.ClientPayments.Sum(payment => payment.PaymentAmount) + InvoiceAmount;
if (TotalPayments > invoice.InvoiceAmount)
{
TempData["PaymentError"] = "You cannot pay more than the invoice amount";
return RedirectToAction("Index");
}
clientPayment.PaymentAmount = InvoiceAmount;
clientPayment.PaymentType = PaymentType;
clientPayment.PaymentDate = DateTime.Now;
db.ClientPayments.Add(clientPayment);
if (TotalPayments != invoice.InvoiceAmount)
{
invoice.InvoiceStatus = "Partly Paid";
}
else
{
invoice.InvoiceStatus = "Confirmed";
}
// You don´t need this, since "invoice" was retrieved earlier in the method the database context
// knows that changes have been made to this object when you call "SaveChanges".
// db.Entry(invoices).State = EntityState.Modified;
db.SaveChanges();
return View();
}
行がときに、フォームが選択されているかどうかを検証する簡単な方法はあります提出されますか?
おかげで、 エイミー
完璧に働いた、ありがとう! – Amy