Home > Software engineering >  MVC 4 - Object reference not set to an instance of an object. when POST action and index each row
MVC 4 - Object reference not set to an instance of an object. when POST action and index each row

Time:11-27

I have two issues in this question

I want to post multiple rows in each table but when I post I get this error:

Object reference not set to an instance of an object. when POST action

in this segment of code:

foreach (var p in hdrdtl.TBHDR)

VS remarks hdrdtl.TBHDR as have the issue.

How can I solve this issue?

My model class:

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; }
    }
}

Controllers

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 = "[email protected]";
                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);
}

View

@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>
}

How can I index each row in the index?

I know there are too many issues but I appreciate your support

CodePudding user response:

try to replace foreach loop by for loop

   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>
           ....          
  • Related