我对一件事有点头疼(我知道以前有人问过类似的问题,但我很确定这不是一回事)。直截了当:我有一个带有 Telerik 网格的视图。在该网格上,我显示了模型中的一些内容,这些内容传递给了视图,但我希望在最后一列中放置一个 CheckBox,该 CheckBox 基于控制器中的某些内容进行检查/取消检查(检查与模型无关正在通过)。在处理视图的 ActionResult 函数中,我将一些Boolean值存储在 ViewData 中,然后根据存储在 ViewData 中的值在 CheckBox 中设置 isChecked 值。
ActionResult 的代码如下:
    [SecureThis(Roles = "User")]
    public ActionResult Index()
    {
        //get bucket ids
        var buckets = db.Buckets.ToList();
        int i=1;
        string cb = "checkbox" + i.ToString();
        foreach (Bucket b in buckets)
        {
            var payByInvoice = db.PaymentOptions.Where(p => p.BucketId == b.Id).Select(p => p.CanPayByInvoice).SingleOrDefault();
            if (payByInvoice == (int)PayByInvoiceState.Accepted)
                ViewData["checkbox" + i.ToString()] = true;
            else ViewData["checkbox" + i.ToString()] = false;
                i++;
                cb = "checkbox" + i.ToString();
        }
        return View(db.Buckets);
    }
应该显示所有内容的网格是这样的:
@{
int i=1;
string cb = "checkbox" + i.ToString();
 }
@(Html.Telerik().Grid(Model)
        .Name("BucketsGrid")
        .DataKeys(keys => keys.Add(bucket => bucket.Id))
        .Columns(
            columns =>
            {
                columns.Template(model => ViewData[model.Id.ToString()])
                    .HeaderTemplate(
                    @<b>@Strings.Title_Customer</b>
                    );
                columns.Bound(model => model.CreditFacility);
                columns.Bound(model => model.Minimum);
                columns.Bound(model => model.RefillLevel);
                columns.Bound(model => model.NotificationEmail);
                columns.Bound(model => model.NotificationSms);
                columns.Template(model => Html.ActionLink(Strings.Edit, "Edit", new { id = model.Id }));
                columns.Template(model => Html.ActionLink(Strings.NotificationOptions, "Bucket", "NotificationOptions", new { id = model.Id }, null));
                columns.Template(model => Html.ActionLink("Refill", "Refill", "Payment", new { id = model.Id }, null));
                columns.Template(model => Html.ActionLink(Strings.Details, "Details", new { id = model.Id }));
                columns.Template(model => Html.ActionLink(Strings.Delete, "Delete", new { id = model.Id }));
                columns.Template(model => Html.CheckBox("invoice", (Boolean)ViewData[@cb])).HeaderTemplate("Invoice Option");                                
                @i++;
                @cb = "checkbox" + i.ToString();
            }
        )
        .Pageable(paging =>
                paging.Enabled(true)
                      .PageSize(UserSettings.GridPageSize)
                      .Style(GridPagerStyles.NextPrevious)
                      .Position(GridPagerPosition.Bottom)
            )
        .Sortable()
        .Scrollable()
        .Resizable(resize=> resize.Columns(true))
)
整个事情的问题在于,无论 ViewData 中存储的数据如何,复选框都未选中。我使用了调试器,并且在 ViewData 中相应地设置了值,但由于某种原因(我还不能说),复选框仍然未被选中。
对此问题的任何想法将不胜感激。