model.Any()上的system.argumentnullexception值不能为null

时间:2016-09-16 13:47:32

标签: asp.net-mvc-4

当我提交表单并且数据库中的表当前为空时,我在Model.Any()行上获得“system.argumentnullexception”。代码如下。

@using System.Linq
@model IEnumerable<Al_sehrawi.Models.tbl_Packing>
@{
ViewBag.Title = "AddBox2";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>AddBox2</h2>

@{

if (Model.Any())
{
    foreach (var i in Model)
    {
    <form action="~/Packing/AddBox2" method="post">
        <table>
            <tr>
                <td>
                    Id:
                    @{int counter = i.packing_id + 1;}
                    <input name="id" type="number" value="@counter" />
                </td>
            </tr>
            <tr>
                <td>
                    Name :
                </td>
                <td>
                    <input name="packingName" type="text" />
                </td>
            </tr>
            <tr>
                <td>
                    Unit :
                </td>
                <td>
                    <input name="unit" type="text" />
                </td>
            </tr>
            <tr>
                <td>
                    Quantity of 1st Packing :
                </td>
                <td>
                    <input name="quantity1" type="number" />
                </td>
            </tr>
            <tr>
            <tr>
                <td>
                    Quantity of 2nd Packing :
                </td>
                <td>
                    <input name="quantity2" type="number" />
                </td>
            </tr>
            <tr>
                <td>
                    <input name="Submit1" type="submit" value="submit" />
                </td>
            </tr>
        </table>

    </form>
                            }
                        }
                        else
                        {
                            <form action="~/Packing/AddBox2" method="post">
                                <table>
                                    <tr>
                                        <td>
                                            Id:

                                            <input name="id" type="number" value="1" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Name :
                                        </td>
                                        <td>
                                            <input name="packingName" type="text" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Unit :
                                        </td>
                                        <td>
                                            <input name="unit" type="text" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Quantity of 1st Packing :
                                        </td>
                                        <td>
                                            <input name="quantity1" type="number" />
                                        </td>
                                    </tr>
                                    <tr>
                                    <tr>
                                        <td>
                                            Quantity of 2nd Packing :
                                        </td>
                                        <td>
                                            <input name="quantity2" type="number" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <input id="Submit1" type="submit" value="submit" />
                                        </td>
                                    </tr>
                                </table>

                            </form>
                                                }
          }

PackingController中的代码如下。

public ActionResult AddBox2()
    {
        PackingModel c = new PackingModel();
        var id = c.getNextId();
        return View(id);
    }
    [HttpPost]
    public ActionResult AddBox2(int id, string packingName, string unit, int quantity1, int quantity2)
    {
        PackingModel p = new PackingModel();
        p.addBox2(id, packingName, unit, quantity1, quantity2);
        return View();
    }

,模型是

public bool addBox2(int id, string packingName, string unit,int quantity1, int quantity2)
    {
        try
        {
            using (POSEntities1 db = new POSEntities1())
            {


                tbl_Packing p = new tbl_Packing();
                tbl_packing_box2a b1 = new tbl_packing_box2a();
                tbl_packing_box2b b2 = new tbl_packing_box2b();

                p.name = packingName;
                p.unit = unit;

                b1.quantiity = quantity1;
                b1.tbl_packing_box_id = id;

                int a_Id;

                    var result = db.tbl_packing_box2a.OrderByDescending(b => b.tbl_packing_box1a_id).Take(1).ToList();
                foreach (var item in result)
                {
                    a_Id = item.tbl_packing_box1a_id;

                    b2.quantity = quantity2;
                    b2.tbl_packing_box2a_id = a_Id;

                    db.tbl_Packing.Add(p);
                    db.tbl_packing_box2a.Add(b1);
                    db.tbl_packing_box2b.Add(b2);
                    db.SaveChanges();
                }
            }
        }
        catch (Exception)
        {

            return false;
        }
        return true;
    }

1 个答案:

答案 0 :(得分:0)

首先检查模型是否为空,然后尝试Model.Any()

@if (Model != null)
{
    if (Model.Any())
    {
    ....
    }
}