错误始终相同:对象引用未设置为控制器中对象的实例

时间:2016-12-09 07:54:11

标签: c# asp.net-mvc

我有问题。我想制作插入方法。我已经检查了所有代码与另一种方法相同的另一种方法是成功的。但在下面的代码中,它总是抛出错误

  

对象引用未设置为对象的实例。

你可以帮帮我吗?我有2天的错误

这是控制器代码

public ActionResult MultipleCommand(NCRForm  ncr, string Command)
{
    if (Command == "Submit")
    {
        int a = 001;
        var filename = Path.GetFileName(ncr.attachment.FileName);
        try
        {

            if (ncr.attachment.ContentLength > (2 * 1024 * 1024))
            {
              ModelState.AddModelError("Custom Error", "File size must be less than 2 MB");
            }
            if (!(ncr.attachment.ContentType == "file/doc" || ncr.attachment.ContentType == "file/docx" || ncr.attachment.ContentType == "file/pdf"))
            {
                ModelState.AddModelError("Custom Error", "File type allowed: doc/docx/pdf");
            }
            var path = Path.Combine(Server.MapPath("~/UploadFile/"), filename);
            ncr.attachment.SaveAs(path);

            // TODO: Add insert logic here
            ViewBag.idNCRCategory = new SelectList(ncrdc.NCR_Categories, "ID", "Category_Name");
            ViewBag.idCountry = new SelectList(ncrdc.Master_Countries, "ID", "Value");
            ViewBag.idShift = new SelectList(ncrdc.Master_Shifts, "ID", "Value");
            ViewBag.idLocation = new SelectList(ncrdc.Master_Locations, "ID", "Value");
            ViewBag.idProcess = new SelectList(ncrdc.Master_Processes, "ID", "Value");
            ViewBag.idProduct = new SelectList(ncrdc.Master_Products, "ID", "Desc");
            ViewBag.idUnit = new SelectList(ncrdc.Master_Units, "ID", "Value");
            ViewBag.idFeedbackCategory = new SelectList(ncrdc.Master_Feedback_Libraries, "ID", "Feedback_LibraryDesc");
            ViewBag.idVendor = new SelectList(ncrdc.Master_Vendors, "ID", "Vendor_Name");
            ViewBag.idProductType = new SelectList(ncrdc.Master_Product_Categories, "ID", "Value");


            debug += "8\n";
            NCR_Form n = new NCR_Form()
            {
                NCR_number = DateTime.Now.ToShortDateString() + "/" + a,
                NCR_status = "Waiting for Production PIC",
                category_name = ncr.idNCRCategory,
                Export = ncr.export,
                Country = ncr.idCountry,
                report_date = ncr.reportDate,
                Problem_date = ncr.problemDate,
                Weeks = ncr.week,
                Years = ncr.year,
                Shifts = ncr.idShift,
                Title = "new NCR",
                Locations = ncr.idLocation,
                Process = ncr.idProcess,
                Product_type = ncr.idProductType,
                Product = ncr.idProduct,
                Production_code = ncr.productionCode,
                OP_Number = ncr.OpNo,
                defect_qty1 = ncr.defectQuantity,
                Unit1 = ncr.idUnit,
                Vendor = ncr.idVendor,
                defect_found_in = ncr.defectFoundIn,
                descriptions = ncr.description,
                //attachFile = "~/UploadFile/" + filename,
                Feedback_Category1 = ncr.idFeedbackCategory,
                Created = DateTime.Now,
                Created_by = DBHelper.GetCurrentUser(HttpContext).Title,
                Modified = DateTime.Now,
                Modified_by = DBHelper.GetCurrentUser(HttpContext).Title
            };
            ncrdc.NCR_Forms.InsertOnSubmit(n);
            ncrdc.SubmitChanges();
            a++;
            return RedirectToAction("Index", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri });

        }
        catch (Exception ex)
        {
            DBHelper.InsertErrorLog("Error: ", ex.Message+debug);
            return Json(ex.Message+ex.StackTrace+debug);
        }

        }

    }
    else
    {
        return RedirectToAction("Index", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri });
    }
}

这是观点:

@model NCRHMSWeb.Models.NCRForm
@using (Html.BeginForm("MultipleCommand", "NCRForm", new { SPHostUrl =        ViewBag.SPURL }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{

//button Action
<div class="row">
    <div class="col-lg-12">
        <div class="ibox float-e-margins">
            <div class="ibox-content">
                <form role="form" class="form-inline">
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="pull-right m-t-4">
                                <button class="btn btn-primary" role="button" type="submit" name="Command" id="Submit" value="Submit">Submit</button>
                                <button class="btn btn-primary" role="button" type="submit" name="Command" id="Save"  value="Save">Save</button>
                                <button class="btn btn-primary" role="button" type="submit" name="Command" id="Cancel" value="Cancel">Cancel</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

//General Section
<div class="row">
    <div class="col-lg-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title tosca-bg">
                <h5>General</h5>
                <div class="ibox-tools">
                    <a class="collapse-link">
                        <i class="fa fa-chevron-up"></i>
                    </a>
                </div>
            </div>
            <div class="ibox-content">
                <form role="form" class="form-inline">
                    <div class="row">
                        <div class="col-lg-6">
                            <div class="row">
                                <div class="col-lg-5">
                                    <label class="control-label">NCR Number</label>
                                </div>
                                <div class="col-lg-7">
                                    <input type="text" class="form-control" name="NCRNumber" id="NCRNumber" value="TBA" contenteditable="false" />
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-6">
                            <div class="row">
                                <div class="col-lg-5">
                                    <label class="control-label"> Status</label>
                                </div>
                                <div class="col-lg-7">
                                    <input type="text" class="form-control" name="NCRStatus" id="NCRStatus" value="New" contenteditable="false" />
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>





//Initiation
<div class="row">
                <div class="col-lg-12">
                    <div class="ibox float-e-margins">
                        <div class="ibox-title tosca-bg topToggle">
                            <h5>Initiation</h5>
                            <div class="ibox-tools">
                                <a class="collapse-link">
                                    <i class="fa fa-chevron-up"></i>
                                </a>
                            </div>
                        </div>
                        <div class="ibox-content typeToggle InitiatorCont">
                            <form role="form" class="form-inline">
                                <div class="row">
                                    <div class="col-lg-6">
                                        <div class="row m-b-2">
                                            <div class="col-lg-5">
                                                <label class="control-label">NCR Category</label>
                                            </div>
                                            <div class="col-lg-7">
                                                @Html.DropDownList("idNCRCategory", ViewBag.idNCRCategory as SelectList, "--Select--", new { @class = "form-control", @required="required" })
                                            </div>
                                        </div>
                                        <div class="row m-b-2">
                                            <div class="col-lg-5">
                                                <label class="control-label">Report Date</label>
                                            </div>
                                            <div class="col-lg-7">
                                                @Html.TextBoxFor(model => model.reportDate, new { @class = "form-control", @Value = DateTime.Now.ToShortDateString()})


                                            </div>
                                        </div>


                                        <div class="row m-b-2">
                                            <div class="col-md-5">
                                                <label class="control-label">Problem Date</label>
                                            </div>
                                            <div class="col-md-7">
                                                <div class="input-group date">
                                                    <span class="input-group-addon">
                                                        <i class="fa fa-calendar"></i>
                                                    </span>
                                                    <input type="datetime" class="form-control" name="problemDate" id="problemDate"  />
                                                </div>
                                            </div>
                                        </div>

                                        <div class="row m-b-2">
                                            <div class="col-md-5">
                                                <label class="control-label">Week/Year</label>
                                            </div>
                                            <div class="col-md-7">
                                                <div class="col-md-5 p-l-0">
                                                    <input type="text" class="form-control" name="week" id="week" style="width:127px;" />
                                                </div>
                                                <div class="col-md-1 p-l-3 p-r-2">
                                                    /
                                                </div>
                                                <div class="col-md-6 p-l-0">
                                                    <input type="text" class="form-control" name="year" id="year" style="width:132px;" />
                                                </div>
                                            </div>
                                        </div>

                                        <div class="row m-b-2">
                                            <div class="col-md-5">
                                                <label class="control-label">Shift</label>
                                            </div>
                                            <div class="col-md-7">
                                                @Html.DropDownList("idShift", ViewBag.idShift as SelectList, "--Select--", new { @class = "form-control", @required = "required" })
                                            </div>
                                        </div>

                                        <div class="row m-b-2">
                                            <div class="col-md-5">
                                                <label class="control-label">Location</label>
                                            </div>
                                            <div class="col-md-7">
                                                @Html.DropDownList("idLocation", ViewBag.idLocation as SelectList, "--Select--", new { @class = "form-control", @required = "required" })
                                            </div>
                                        </div>

                                        <div class="row m-b-2">
                                            <div class="col-md-5">
                                                <label class="control-label">Process</label>
                                            </div>
                                            <div class="col-md-7">
                                                @Html.DropDownList("idProcess", ViewBag.idProcess as SelectList, "--Select--", new { @class = "form-control", @required = "required" })
                                            </div>
                                        </div>
                                    </div>


                                    <div class="col-lg-6">
                                        <div class="row m-b-2">
                                            <div class="col-lg-5">
                                                <input type="checkbox" name="export" id="export" /><label class="control-label p-l-2">Export</label>
                                            </div>
                                            <div class="col-lg-7" id="divCountry">
                                                <div class="col-lg-3 p-l-0">
                                                    <label class="control-label">Country</label>
                                                </div>
                                                <div class="col-lg-9 p-l-2">
                                                    @Html.DropDownList("idCountry", ViewBag.idCountry as SelectList, "--Select--", new { @class = "form-control dropDown", @required = "true" })
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row m-b-2">
                                            <div class="col-md-5">
                                                <label class="control-label">Product Type</label>
                                            </div>
                                            <div class="col-md-7">
                                                @Html.DropDownList("idProductType", ViewBag.idProductType as SelectList, "--Select--", new { @class = "form-control" })
                                            </div>
                                        </div>

                                        <div class="row m-b-2">
                                            <div class="col-md-5">
                                                <label class="control-label">Product</label>
                                            </div>
                                            <div class="col-md-7">
                                                @Html.DropDownList("idProduct", ViewBag.idProduct as SelectList, "--Select--", new { @class = "form-control" })
                                            </div>
                                        </div>

                                        <div class="row m-b-2">
                                            <div class="col-md-5">
                                                <label class="control-label">ProductionCode/OpNo</label>
                                            </div>
                                            <div class="col-md-7">
                                                <div class="col-md-5 p-l-0">
                                                    <input type="text" class="form-control" name="productionCode" id="productionCode" style="width:127px;" required="required" />
                                                </div>
                                                <div class="col-md-1 p-l-3 p-r-2">
                                                    /
                                                </div>
                                                <div class="col-md-6 p-l-0">
                                                    <input type="text" class="form-control" name="OpNo" id="OpNo" style="width:132px;" required="required"/>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="row m-b-2">
                                            <div class="col-md-5">
                                                <label class="control-label">DefectQty/Unit</label>
                                            </div>
                                            <div class="col-md-7">
                                                <div class="col-md-5 p-l-0">
                                                    <input type="text" class="form-control" name="defectQuantity" id="defectQuantity" style="width:127px;" required="required"/>
                                                </div>
                                                <div class="col-md-1 p-l-3 p-r-2">
                                                    /
                                                </div>
                                                <div class="col-md-6 p-l-0">
                                                    @Html.DropDownList("idUnit", ViewBag.idUnit as SelectList, "--Select--", new { @class = "form-control" })
                                                </div>
                                            </div>
                                        </div>

                                        <div class="row m-b-2">
                                            <div class="col-md-5">

                                            </div>
                                            <div class="col-md-7">
                                                <div class="col-md-5 p-l-0">
                                                    <input type="text" class="form-control" name="defectQuantity" id="defectQuantity" style="width:127px;" />
                                                </div>
                                                <div class="col-md-1 p-l-3 p-r-2">
                                                    /
                                                </div>
                                                <div class="col-md-6 p-l-0">
                                                    @Html.DropDownList("idUnit", ViewBag.idUnit as SelectList, "--Select--", new { @class = "form-control" })
                                                </div>
                                            </div>
                                        </div>

                                        <div class="row m-b-2">
                                            <div class="col-md-5">
                                                <label class="control-label">Vendor</label>
                                            </div>
                                            <div class="col-md-7">
                                                @Html.DropDownList("idVendor", ViewBag.idVendor as SelectList, "--Select--", new { @class = "form-control" })
                                            </div>
                                        </div>

                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-12">
                                        <div class="row m-b-2">
                                            <div class="col-lg-2">
                                                <label class="control-label">Defect Found In</label>
                                            </div>
                                            <div class="col-lg-10">
                                                <div class="col-lg-4">
                                                    <input type="radio" class="form-control" id="IsRaw" name="defectFoundIn" />Raw Material
                                                    @*@Html.RadioButton("defectFoundIn", "RawMaterial", true, new { id = "IsRaw" }) Raw Material*@
                                                </div>
                                                <div class="col-lg-4">
                                                    <input type="radio" class="form-control" id="IsProduct" name="defectFoundIn" />Product during Processing
                                                    @*@Html.RadioButton("defectFoundIn", "ProductduringProcessing", true, new { id = "IsProduct" }) Product during Processing*@
                                                </div>
                                                <div class="col-lg-4">
                                                    <input type="radio" class="form-control" id="IsEnd" name="defectFoundIn" />End Product
                                                    @*@Html.RadioButton("defectFoundIn", "EndProduct", true, new { id = "IsEnd" }) End Product*@
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row m-b-3">
                                            <div class="col-lg-2">
                                                <label class="control-label">Feedback Category 1</label>
                                            </div>
                                            <div class="col-lg-10">
                                                @Html.DropDownList("idfeedbackCategory", ViewBag.idfeedbackCategory as SelectList, "--Select--", new { @class = "form-control dropDown-long" })
                                            </div>
                                        </div>
                                        <div class="row m-b-2">
                                            <div class="col-lg-2">
                                                <label class="control-label">Feedback Category 2</label>
                                            </div>
                                            <div class="col-lg-10">
                                                @Html.DropDownList("idfeedbackCategory", ViewBag.idfeedbackCategory as SelectList, "--Select--", new { @class = "form-control dropDown-long" })
                                            </div>
                                        </div>
                                        <div class="row m-b-2">
                                            <div class="col-lg-2">
                                                <label class="control-label">Feedback Category 3</label>
                                            </div>
                                            <div class="col-lg-10">
                                                @Html.DropDownList("idfeedbackCategory", ViewBag.idfeedbackCategory as SelectList, "--Select--", new { @class = "form-control dropDown-long" })
                                            </div>
                                        </div>
                                        <div class="row m-b-2">
                                            <div class="col-lg-2">
                                                <label class="control-label">Feedback Category 4</label>
                                            </div>
                                            <div class="col-lg-10">
                                                @Html.DropDownList("idfeedbackCategory", ViewBag.idfeedbackCategory as SelectList, "--Select--", new { @class = "form-control dropDown-long" })
                                            </div>
                                        </div>
                                        <div class="row m-b-2">
                                            <div class="col-lg-2">
                                                <label class="control-label">Feedback Category 5</label>
                                            </div>
                                            <div class="col-lg-10">
                                                @Html.DropDownList("idfeedbackCategory", ViewBag.idfeedbackCategory as SelectList, "--Select--", new { @class = "form-control dropDown-long" })
                                            </div>
                                        </div>
                                        <div class="row m-b-2">
                                            <div class="col-lg-2">
                                                <label class="control-label">Description</label>
                                            </div>
                                            <div class="col-lg-10">
                                                <textarea class="form-control textarea-large" id="description" name="description"></textarea>
                                            </div>
                                        </div>
                                        <div class="row m-b-2">
                                            <div class="col-lg-2">
                                                <label class="control-label">Attachment</label>
                                            </div>
                                            <div class="col-lg-10">
                                                <input type="file" class="form-control" id="attachment" name="attachment" />
                                            </div>
                                        </div>
                                        <div class="row m-b-2">
                                            <div class="col-lg-2">
                                                <label class="control-label">Production PIC</label>
                                            </div>
                                            <div class="col-lg-10">
                                                @*@Html.DropDownList("select");*@
                                                <input type="text" />
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
}

0 个答案:

没有答案