提问人:maleficent 提问时间:6/9/2022 更新时间:6/9/2022 访问量:125
asp.net 核心 2.1 未将属性绑定到模型 onPost
asp.net core 2.1 not binding properties to model onPost
问:
Report.cshtml(报告.cshtml)
@{
@if (Model.ShowResults)
{
@if (Model.myCount > 0)
{
<div id="divResults">
<table id="tbl1">
<thead class="thead-dark customcolor1">
<tr>
<th>Category</th>
<th>Year</th>
<th>Month</th>
<th>Type</th>
<th>Batch No</th>
<th class="text-right">Amount</th>
</tr>
</thead>
<tbody class="cfont">
@foreach (TestHelp.Classes.TestReportModel item in Model.ReportLists)
{
<tr>
<td class="result">
@item.Category
</td>
<td class="result">
@item.Year
</td>
<td class="result">
@item.Month
</td>
<td class="result">
@item.Type
</td>
<td class="result">
@item.BatchNo
</td>
<td>
@String.Format("{0:0000000000.00000}", @item.AccountNo)
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="row border-top border-dark">
<div class="col pt-2 text-left">
<b>Reported By: </b>@ViewData["reportedBy"]
</div>
<div class="col pt-2 text-right">
<b>Reported On: </b>@ViewData["reportDate"]
</div>
</div>
}
else if (Model.errorText.ToString() != "")
{
<div class="row border-dark border-top">
<div class="col pt-2 text-center">
<table id="tblZeroRecords" class="text-center">
<tr>
<td>
<p class="text-danger">@ViewData["errorMessage"]</p>
</td>
</tr>
</table>
</div>
</div>
<div class="row border-top border-dark">
<div class="col pt-2 text-left">
<b>Reported By: </b>@ViewData["reportedBy"]
</div>
<div class="col pt-2 text-right">
<b>Reported On: </b>@ViewData["reportDate"]
</div>
</div>
}
else
{
<div class="row border-dark border-top">
<div class="col pt-2 text-center">
<table id="tblZeroRecords" class="text-center">
<tr>
<td>
<p class="text-danger">No Data found</p>
</td>
</tr>
</table>
</div>
</div>
<div class="row border-top border-dark">
<div class="col pt-2 text-left">
<b>Reported By: </b>@ViewData["reportedBy"]
</div>
<div class="col pt-2 text-right">
<b>Reported On: </b>@ViewData["reportDate"]
</div>
</div>
}
}
}
Report.cshtml.cs
public class ReportModel : PageModel
{
[BindProperty]
public List<ReportModel> ReportLists { get; set; }
[BindProperty]
public bool ShowResults { get; set; }
[BindProperty]
public bool myFlag { get; set; }
public string errorText { get; set; }
[BindProperty]
public int myCount { get; set; }
private readonly ReportContext _context;
public ReportModel(ReportContext context)
{
_context = context;
}
[BindProperty]
public Report Reports { get; set; }
public void OnPost([Bind("textFromDate,textToDate")] Report report)
{
int year;
int month;
DateTime reportDate = DateTime.Now;
if (ModelState.IsValid)
{
if (report.textToDate < report.textFromDate)
{
report.textToDate = report.textFromDate;
}
string textFromDate = report.textFromDate.Date.ToString("MM/dd/yyyy");
string textToDate = report.textToDate.Date.ToString("MM/dd/yyyy");
int.TryParse(report.textToDate.ToString("yyyy"), out year);
int.TryParse(report.textToDate.ToString("MM"), out month);
int lastday = DateTime.DaysInMonth(year, month);
textToDate = month.ToString() + "/" + lastday.ToString() + "/" + year.ToString();
ShowResults = true;
try
{
List<ReportModel> records = _context.Report(textFromDate,textToDate).ToList();
ReportLists = records;
myCount = records.Count();
if (myCount > 0)
myFlag = true;
else
myFlag = false;
}
catch (Exception ex)
{
errorText = "Internal error";
}
}
string UserName = HttpContext.User.Identity.Name.ToString().Replace("MMC\\", "").ToUpper();
if(UserName == "" || UserName == null)
{
UserName = "DEFAULT";
}
ViewData["reportedBy"] = UserName;
ViewData["reportDate"] = reportDate;
ViewData["errorMessage"] = errorText;
ViewData["count"] = myCount;
}
}
现在我的问题是,当我单击提交时,它会转到 onPost 并成功获取数据,但即使 myCount 为 0,Report.cshtml @if(Model.myCount > 0) 中的 if 块也会执行,因此 @ViewData[“reportedDate”] 抛出空引用异常。请帮助我解决这个问题。我已经尝试了所有方法,以便在 myCount=0 时 if 块不会执行。
答: 暂无答案
评论
ViewData["reportDate"] = reportDate;
@ViewData["reportedDate"]
reportDate
datetime