简体   繁体   English

更新选定的行实体框架MVC 3

[英]Update Selected Row Entity Framework MVC 3

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. 我还希望将新的付款条目添加到与所选发票相关的ClientPayments表中,我希望PaymentAmount是单选按钮金额,而日期是系统日期。

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'. 我目前拥有的代码产生一个错误,即:参数字典包含一个非空类型'System.Int32'的参数'id'的空条目。

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. javascript警报会显示我选择的正确ID,但是当我单击“确认”时,找不到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; 1)为使回发正常工作,您需要正确提交表单,例如在表单内有一个提交按钮;
2) For adding a new entry to the ClientPayments table, you do something along the line of 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM