MVC 4 - 未将对象引用设置为对象的实例。当 POST 操作并索引每一行
MVC 4 - Object reference not set to an instance of an object. when POST action and index each row
这个问题我有两个问题
我想 post 每行 table 但是当我 post 我得到这个错误:
Object reference not set to an instance of an object. when POST action
在这段代码中:
foreach (var p in hdrdtl.TBHDR)
VS 评论 hdrdtl.TBHDR
有问题。
我该如何解决这个问题?
我的模型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; }
}
}
控制器
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>
}
如何索引 table 中的每一行?
我知道问题太多,但感谢您的支持
尝试用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>
....
这个问题我有两个问题
我想 post 每行 table 但是当我 post 我得到这个错误:
Object reference not set to an instance of an object. when POST action
在这段代码中:
foreach (var p in hdrdtl.TBHDR)
VS 评论 hdrdtl.TBHDR
有问题。
我该如何解决这个问题?
我的模型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; }
}
}
控制器
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>
}
如何索引 table 中的每一行?
我知道问题太多,但感谢您的支持
尝试用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>
....