我对某件事情感到头疼(我知道以前曾经问过的事情,但我很确定这不是一回事)。
要点:
我有一个Telerik网格视图。在那个网格上我展示了一些来自模型的东西,我传递给View但是我想在最后一列中放置一个CheckBox,它根据Controller中的一些东西进行检查/取消选中(这些检查与模型无关)正在通过)。在我的ActionResult函数中,它负责View I在ViewData中存储一些Boolean
值,然后根据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中相应,但由于某种原因(我还不能说),复选框仍然保持未选中状态。
非常感谢有关此事的任何想法。
答案 0 :(得分:0)
我发现了这一切的问题。正如所料,这是我自己做的(或者说是这样)。问题是我在Telerik网格声明中增加了@i变量,认为它会发生在网格中的所有行中,但是这个东西只触发一次。因此,ViewData[@cb]
值将始终在Controller中设置第二个值(在我的情况下为false
),然后将取消选中所有复选框。
修复:
我使用ViewBag并使用Dictionary<Guid, bool>
进行设置以保存我的值,并使用model.Id
属性迭代它。对于可能感兴趣的任何人,我将发布以下代码。
控制器:
ViewBag.Dict = new Dictionary<Guid, bool>();
Dictionary<Guid, bool> dict = new Dictionary<Guid, bool>();
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)
{
dict.Add(b.Id, false);
}
if (payByInvoice == (int)PayByInvoiceState.Accepted)
{
dict.Add(b.Id, true);
}
}
ViewBag.Dict = dict;
查看:
columns.Template(model => Html.CheckBox("invoice", (bool)ViewBag.Dict[model.Id])).HeaderTemplate("Invoice option");