单击用 mvc C# 填写的表单上的提交时的空引用

null reference when clicking submit on a form that is filled in with mvc C#

我有一个在视图中创建的表单,该表单应该使用视图模型提交给两个表。当我填写字段并单击提交时,在填写表单中的所有内容后,我在 vendormodel 和 refvendormodel 上收到空引用异常,我不确定原因。非常感谢任何帮助。

我的控制器方法:

      [HttpPost]
       [Route("Contract/AddVendor")]
       [Route("Contract/AddVendor/{addVendor}")]
    
        public IActionResult AddVendorForm(AddVendorVM addVendor)
        {
            AddVendorVM model = new AddVendorVM();
    
          if(ModelState.IsValid)
{
                model.vendormodel.VENDOR_ID = addVendor.vendormodel.VENDOR_ID;
                model.refvendormodel.VENDOR_ID = addVendor.refvendormodel.VENDOR_ID;
                model.vendormodel.VENDOR_NAME = addVendor.vendormodel.VENDOR_NAME;
                model.refvendormodel.ADDRESS = addVendor.refvendormodel.ADDRESS;
                model.refvendormodel.CITY = addVendor.refvendormodel.CITY;
                model.refvendormodel.STATE = addVendor.refvendormodel.STATE;
                model.refvendormodel.ZIP = addVendor.refvendormodel.ZIP;
                model.refvendormodel.FIRST_NAME = addVendor.refvendormodel.FIRST_NAME;
                model.refvendormodel.LAST_NAME = addVendor.refvendormodel.LAST_NAME;
                model.refvendormodel.PHONE = addVendor.refvendormodel.PHONE;
                
                _context.VENDORs.Add(model.vendortmodel);
                _context.REF_VENDORs.Add(model.refvendormodel);
                _context.SaveChangesAsync();
                return RedirectToAction("Edit");
    }
          
            return View(model);
        }
    
       
       [HttpGet]
        [Route("Contract/AddVendor")]

        public IActionResult AddRespondentForm()
        {
            AddVendorVM model = new AddVendorVM();
            return View(model);

        }

     

我的看法:

    <div class="col-md-4">
        @using (
        Html.BeginForm("AddVendor", "Contract", FormMethod.Post, new { id = "Form1"}))
        {
            @Html.HiddenFor(m => Model.vendormodel.VENDOR_ID, new { id = "VendorId" })
            @Html.HiddenFor(m => Model.refvendormodel.VENDOR_ID, new { id = "VendorId" })
    
            <label class="control-label">Vendor Name:</label>
            @Html.TextBoxFor(r => r.vendormodel.VENDOR_NAME)
    
    
            <label class="control-label">Address:</label>
            @Html.TextBoxFor(r => r.refvendormodel.ADDRESS)
    
    
    
            <label class="control-label">City:</label>
            @Html.TextBoxFor(r => r.refvendormodel.CITY)
    
    
    
            <label class="control-label">State:</label>
            @Html.TextBoxFor(r => r.refvendormodel.STATE)
    
    
    
            <label class="control-label">Zip Code:</label>
            @Html.TextBoxFor(r => r.refvendormodel.ZIP)
    
    
    
            <label class="control-label">First Name:</label>
            @Html.TextBoxFor(r => r.refvendormodel.FIRST_NAME)
    
    
    
            <label class="control-label">Last Name:</label>
            @Html.TextBoxFor(r => r.refvendormodel.LAST_NAME)
    
    
    
            <label class="control-label">Phone Number:</label>
            @Html.TextBoxFor(r => r.refvendormodel.PHONE)
    
    
            <br />
            <input type="submit" value="Add Vendor" />
        }
    </div>
    
    
    <div>
        <a asp-action="Index">Back to List</a>
        </div>

我的模特:

        public class AddVendorVM
    
        {
    
    
            public VENDOR vendormodel { get; set; }
            public REF_VENDOR refvendormodel { get; set; }
    
    
        }
    

您需要像下面这样更改您的代码:

 public IActionResult AddVendorForm(AddVendorVM addVendor)
    {
        ModelState.Remove("vendormodel.VENDOR_ID");
        ModelState.Remove("refvendormodel.VENDOR_ID");
        if (ModelState.IsValid)
        {
            VENDOR model1 = addVendor.vendormodel;
            REF_VENDOR model2 = addVendor.refvendormodel;
            _context.VENDORs.Add(model1);
            _context.REF_VENDORs.Add(model2);
            _context.SaveChanges();
            return RedirectToAction("Edit");
        }

        return View();         
    }

您认为的代码无意义,您可以删除它们:

@Html.HiddenFor(m => Model.vendormodel.VENDOR_ID, new { id = "VendorId" })
@Html.HiddenFor(m => Model.refvendormodel.VENDOR_ID, new { id = "VendorId" })

请注意,如果你的VENDOR_ID是模型VENDOR的主键,REF_VENDOR。它将是必填列并自动增加,所以如果VENDOR_ID0,验证永远不会为真,因此您需要删除 vendormodel.VENDOR_ID 模型状态,然后模型状态将为真。

并且您需要注意,在 synchronous method.That 中不使用 SaveChangesAsync 将不起作用。