在 Razor 视图中访问二维数组

Accessing a 2D array in a Razor View

我正在尝试创建一个预订表格,用户可以在其中选择一个小时时段。 我的想法是创建一个 ViewBag,它将是一个二维数组,其中将包含一个 Booking 对象或一个空引用。然后我可以在视图中使用 Razor 构建一个 table。每个将包含预订信息(如果存在)或 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

检查你的拼写。在您的控制器代码中,您有 ViewBag.Diary = weekBooking。在您看来,您拼写日记的方式不同。 (@if(ViewBag.Dairy[x,d] != null)。您可能还想考虑使用 null 条件运算符来防止异常。您可以检查 null 并记录发现空值与获取异常页面。

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators