【问题标题】:Update Selected Row Entity Framework MVC 3更新选定的行实体框架 MVC 3
【发布时间】:2011-11-10 17:38:28
【问题描述】:

我希望能够在表格中选择一行(代表发票),选择具有金额值的单选按钮,然后按“确认”。单击确认后,我希望所选发票的状态根据选择的付款金额更改为“已付款”或“部分付款”。我还希望将新的付款条目添加到与所选发票相关的 ClientPayments 表中,我希望 PaymentAmount 为单选按钮金额,日期为系统日期。

我目前的代码正在产生错误,即:参数字典包含不可为空类型“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' 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>


确认付款

而控制器中的代码是:

    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。谁能看出我哪里出错了?

谢谢, 艾米

【问题讨论】:

  • 包含您使用 $('#id') 选择的 id 属性的元素是什么样的?
  • 元素应该是通过按“选择”链接选择的任何行。喜欢 Select
  • 所以'id'必须返回所选行的InvoiceNumberID
  • 我为我的问题道歉,看来我在我的 android 手机上使用的 opera 浏览器对提供的 Html 进行了无效渲染。 80% 的视图代码没有显示出来。
  • 在您提供的视图代码中,您的提交按钮在哪里?或者你如何提交你的表格?我刚刚对其进行了测试(在表单中放置了一个提交按钮)并且代码运行良好。

标签: c# asp.net-mvc-3 entity-framework-4 radio-button


【解决方案1】:

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();

【讨论】:

  • 非常感谢!还有一个问题 - 我如何将 PaymentDate 设置为当前系统日期?有没有一些简单的说法 clientPayment.PaymentDate = sort sore of now date
  • Here你可以找到更多信息
  • 我的意思是clientPayments.PaymentDate = systemdate?我没有意识到星号的格式很奇怪
猜你喜欢
  • 1970-01-01
  • 2014-09-07
  • 2014-03-31
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多