MVC 4 - 对象引用未设置为对象的实例。当 POST 动作和索引每一行时

Posted

技术标签:

【中文标题】MVC 4 - 对象引用未设置为对象的实例。当 POST 动作和索引每一行时【英文标题】:MVC 4 - Object reference not set to an instance of an object. when POST action and index each row 【发布时间】:2022-01-03 23:50:35 【问题描述】:

这个问题我有两个问题

我想在每个表中发布多行,但是当我发布时出现此错误:

对象引用未设置为对象的实例。当 POST 动作时

在这段代码中:

foreach (var p in hdrdtl.TBHDR)

VS 评论hdrdtl.TBHDR 有问题。

我该如何解决这个问题?

我的模型课:

namespace Mvc_CSS.Models

    public class HeaderDetailModels
    
        public virtual List<TB_RST_SVCHDR> TBHDR  get; set; 
        public virtual List<TB_RST_SVCDTL> TBDTL  get; set; 
    

控制器

public ActionResult Index()

    var hdrdtl = new HeaderDetailModels
        
            TBHDR = new List<Mvc_CSS.TB_RST_SVCHDR>  new TB_RST_SVCHDR  REQ_NO = 0, REQUESTOR_EMPNAME = "", REQUESTOR_EMPNO = "", DEPT_CD = "", ORG_NAME = "", EMAIL_ID = "", APPROVER1 = "", APPROVER2 = "", TOTAL_AMOUNT = 0, REQUEST_DATE = DateTime.Now, APPROVAL_DATE = DateTime.Now, EVENT_DATE = DateTime.Now, EVENT_PLACE = "", PURPOSE = "", REMARKS = "", STATUS = "", EXT = ""  ,
            TBDTL = new List<Mvc_CSS.TB_RST_SVCDTL>  new TB_RST_SVCDTL  REQ_NO = 0, SEQ_NO = 0, ITEM_ID = 0, QUANTITY = 0, UOM = "", UNIT_PRICE = 0, EXTENDED_AMT = 0  ,
        ;
        
    // List<Mvc_CSS.TB_RST_SVCHDR> hdr = new List<Mvc_CSS.TB_RST_SVCHDR>  new TB_RST_SVCHDR  REQ_NO = 0, REQUESTOR_EMPNAME = "", REQUESTOR_EMPNO = "", DEPT_CD = "", ORG_NAME = "", EMAIL_ID = "", APPROVER1 = "", APPROVER2 = "", TOTAL_AMOUNT = 0, REQUEST_DATE = DateTime.Now, APPROVAL_DATE = DateTime.Now, EVENT_DATE = DateTime.Now, EVENT_PLACE = "", PURPOSE = "", REMARKS = "", STATUS = "", EXT = ""  ;

    Entities db = new Entities();

    return View(hdrdtl);


[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(HeaderDetailModels hdrdtl)

    Entities db = new Entities();

    if (ModelState.IsValid)
    
        using (Entities cd = new Entities())
        
            foreach (var p in hdrdtl.TBHDR)
            
                p.STATUS = "N";
                p.CRT_DATE = DateTime.Now;
                p.APPROVER3 = "saul.gomez@lge.com";
                cd.TB_RST_SVCHDR.Add(p);
            

            cd.SaveChanges();
            ModelState.Clear();

            hdrdtl = new HeaderDetailModels
                
                    TBHDR = new List<Mvc_CSS.TB_RST_SVCHDR>  new TB_RST_SVCHDR  REQ_NO = 0, REQUESTOR_EMPNAME = "", REQUESTOR_EMPNO = "", DEPT_CD = "", ORG_NAME = "", EMAIL_ID = "", APPROVER1 = "", APPROVER2 = "", TOTAL_AMOUNT = 0, REQUEST_DATE = DateTime.Now, APPROVAL_DATE = DateTime.Now, EVENT_DATE = DateTime.Now, EVENT_PLACE = "", PURPOSE = "", REMARKS = "", STATUS = "", EXT = ""  ,
                    TBDTL = new List<Mvc_CSS.TB_RST_SVCDTL>  new TB_RST_SVCDTL  REQ_NO = 0, SEQ_NO = 0, ITEM_ID = 0, QUANTITY = 0, UOM = "", UNIT_PRICE = 0, EXTENDED_AMT = 0  ,
                ;
        
    

    return View(hdrdtl);

查看

@model Mvc_CSS.Models.HeaderDetailModels

@using (html.BeginForm("Index","Home", FormMethod.Post, new  id = "fdata" ))
 
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <table>
        @if (Model != null && Model.TBHDR.Count > 0)
        
            int m = 0;
            foreach (var p in Model.TBHDR)
            
        <thead>
            <th>Date & Time Requested</th>
            <th>Purpose</th>
            <th>Extension</th>
        </thead>
        <tbody>
            <td>@Html.TextBoxFor(mod => p.REQUEST_DATE)</td>
            <th>@Html.TextBoxFor(mod => p.PURPOSE)</th>
            <th>@Html.TextBoxFor(mod => p.EXT)</th>
            <th></th>
            <th></th>
        </tbody>
                m++;
            
        
    </table>
            <center><input type="submit" value="Submit Request" id="semail"/></center>
    

</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>

如何索引表格中的每一行?

我知道问题太多,但感谢您的支持

【问题讨论】:

"如何索引索引中的每一行?"这是什么意思? 对不起,我写错了问题 【参考方案1】:

尝试用for循环替换foreach循环

   for (var i=0; i< Model.TBHDR.Count; i++)
   
             ....

            <td>@Html.TextBoxFor(model => model.TBHDR[i].REQUEST_DATE)</td>
            <th>@Html.TextBoxFor(model => model.TBHDR[i].PURPOSE)</th>
            <th>@Html.TextBoxFor(model => model.TBHDR[i].EXT)</th>
           ....          

【讨论】:

以上是关于MVC 4 - 对象引用未设置为对象的实例。当 POST 动作和索引每一行时的主要内容,如果未能解决你的问题,请参考以下文章

对象引用未设置为 DataGridView 的 InitializeEditingControlValue 中的对象实例

Blazor Webassembly:对象引用未设置为对象 i 的实例

Roles.GetRolesForUser 抛出异常对象引用未设置为对象的实例

Blazor WASM 部署到 IIS 对象引用未设置为对象的实例

Soap 错误:“服务器无法处理请求”“对象引用未设置为对象的实例”

未将对象引用设置到对象的实例都可能由哪些原因引起的