我正在尝试创建一个预订表单,用户可以在其中选择一个小时时段。 我的想法是创建一个ViewBag,它将是一个2d数组,其中将包含一个Booking对象或一个空引用。然后,我可以在视图中使用Razor构建表。每个数组将包含预订信息(如果存在)或DateTime(如果检测到对数组的空引用)。单击DateTime单元格将使用javascript在表单中提供DateTime值,以提供预订信息。
我当前的问题是我无法从视图内部访问数组。
任何关于更好方法的建议将不胜感激。
这是我到目前为止的内容: 视图:
@model Booking
<link rel="stylesheet" href="/Styles/Calendar.css"/>
<div>
<form action="Book" method="POST">
<div class="form-group">
<label asp-for="User"></label>
<input asp-for="User" class="form-control"/>
<button type="submit">Submit</button>
</div>
<input asp-for="Slot" id="Slot" hidden="hidden"></p>
</form>
<table class="table table-bordered">
<tr>
<th></th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Sunday</th>
</tr>
<tr>
<th></th>
@for(int day = 0; day < 7; day++){
<th>@ViewBag.WkStart.AddDays(day).ToString("d")</th>
}
</tr>
@for(int x = 0; x < 12; x++){
<tr>
<td>@ViewBag.WkStart.AddHours(x).ToString("h tt")</td>
@for(int d = 0; d < 7; d++){
@if(ViewBag.Dairy[x,d] != null){
<td>ViewBag.Diary[x,d].User</td>
}
else{
<td onclick="SelectDate(this)">ViewBag.WkStart.AddDays(d).AddHours(x)</td>
}
}
</tr>
}
</table>
</div>
<script src="/Scripts/Calendar.js"></script>
控制器:
public class BookingController : Controller{
private IRepository repository;
private Booking[,] weekBooking = new Booking[12,7];
public BookingController(IRepository repo){
repository = repo;
}
public ViewResult Calendar(){
DateTime date = DateTime.Now.Date.AddHours(6);
DateTime wkStart;
switch(date.DayOfWeek){
case DayOfWeek.Monday:
wkStart = date;
break;
case DayOfWeek.Tuesday:
wkStart = date.AddDays(-1);
break;
case DayOfWeek.Wednesday:
wkStart = date.AddDays(-2);
break;
case DayOfWeek.Thursday:
wkStart = date.AddDays(-3);
break;
case DayOfWeek.Friday:
wkStart = date.AddDays(-4);
break;
case DayOfWeek.Saturday:
wkStart = date.AddDays(-5);
break;
case DayOfWeek.Sunday:
wkStart = date.AddDays(-6);
break;
default:
wkStart = date;
break;
}
ViewBag.WkStart = wkStart;
/* test booking */
Booking trial = new Booking {
BookingID = 111,
User = "Dan",
Slot = DateTime.Now.Date.AddHours(12)
};
weekBooking[1,1] = trial;
ViewBag.Diary = weekBooking;
return View();
}
}
编辑:添加了错误消息。
错误消息:
An unhandled exception occurred while processing the request.
RuntimeBinderException: Cannot perform runtime binding on a null reference
CallSite.Target(Closure , CallSite , object , int , int )
Stack Query Cookies Headers
RuntimeBinderException: Cannot perform runtime binding on a null reference
CallSite.Target(Closure , CallSite , object , int , int )
System.Dynamic.UpdateDelegates.UpdateAndExecute3<T0, T1, T2, TRet>(CallSite site, T0 arg0, T1 arg1, T2 arg2)
AspNetCore._Views_Booking_Calendar_cshtml+<ExecuteAsync>d__15.MoveNext() in Calendar.cshtml
@if(ViewBag.Dairy[x,d] != null){
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
屏幕截图可提供更好的视图: ErrorScreenshot
答案 0 :(得分:0)
检查您的拼写。在您的控制器代码中,您有ViewBag。 Diary = weekBooking。在您看来,日记的拼写方式有所不同。 (@if(ViewBag。 Dairy [x,d]!= null)。同样,您可能要考虑使用null条件运算符来防止异常。您可以检查null并记录为null值被发现而不是获得例外页面。