Asp.net mvc 在MVC jqGrid中添加激活/停用按钮
我的项目中有FormTemplate类Asp.net mvc 在MVC jqGrid中添加激活/停用按钮,asp.net-mvc,jqgrid-asp.net,mvcjqgrid,Asp.net Mvc,Jqgrid Asp.net,Mvcjqgrid,我的项目中有FormTemplate类 public class FormTemplate : BaseEntity { public virtual string Name { get; set; } public virtual DateTime? DateCreation { get; set; } public virtual FormTemplateGroup Group { get; set; } public virtual bool Active
public class FormTemplate : BaseEntity
{
public virtual string Name { get; set; }
public virtual DateTime? DateCreation { get; set; }
public virtual FormTemplateGroup Group { get; set; }
public virtual bool Active { get; set; }
public virtual FormTemplateStatus Status { get; set; }
public virtual IList<QuestionBlock> QuestionBlocks { get; set; }
public virtual bool IsFreeze { get; set; }
}
我没有在我的页面上显示IsFreeze
属性的值,但是如果IsFreeze==true,我需要添加Activate按钮,否则需要为每个表单模板禁用按钮
我尝试在buttonize
function buttonize(cellvalue, options, rowobject) {
var result = '<input type="button" value="Edit" onclick="editTemplate(' + options.rowId + ')">' + ' '
+ '<input type="button" value="Delete" onclick="deleteTemplate(' + options.rowId + ')">' + ' ';
if (isFreezeTemplate(rowobject[4])) {
result += '<input type="button" value="Activate" onclick="activateTemplate(' + options.rowId + ')">';
}
else {
result += '<input type="button" value="Deativate" onclick="deactivateTemplate(' + options.rowId + ')">';
}
return result;
}
并添加到控制器中
[HttpPost]
public bool IsFreezeFormTemplate(int id)
{
var formTemplate =
FormTemplateRepository.Query()
.Where(ft => ft.Id == id)
.SingleOrDefault();
if (formTemplate.IsFreeze == true) return true;
return false;
}
但是我的页面上只有所有表单模板的激活按钮。
如何修复它 您可以向网格中添加一个隐藏列,从中通过rowobject参数读取buttonize函数中的值
.AddColumn(new Column("IsFreeze").SetHidden(true))
这样,您就不需要ajax请求。我建议您在FormTemplateGridData
操作中填写IsFreeze
(如果未完成),并直接在按钮化功能中使用它:
function buttonize(cellvalue, options, rowobject) {
var result = '<input type="button" value="Edit" onclick="editTemplate(' + options.rowId + ')">' + ' '
+ '<input type="button" value="Delete" onclick="deleteTemplate(' + options.rowId + ')">' + ' ';
if (rowobject["IsFreeze"]) {
result += '<input type="button" value="Activate" onclick="activateTemplate(' + options.rowId + ')">';
}
else {
result += '<input type="button" value="Deactivate" onclick="deactivateTemplate(' + options.rowId + ')">';
}
return result;
}
函数按钮化(单元格值、选项、行对象){
变量结果=“”+“”
+ '' + ' ';
if(rowobject[“IsFreeze”]){
结果+='';
}
否则{
结果+='';
}
返回结果;
}
编辑-操作代码
为了精确地确定要发送到网格的内容,以下是网格数据操作的代码:
public JsonResult FormTemplateGridData()
{
var donnees = new
{
total = 2,
page = 1,
records = 2,
rows = new List<FormTemplate>
{
new FormTemplate { Id = 1, Active = true, Name = "first", IsFreeze = true },
new FormTemplate { Id = 2, Active = true, Name = "second", IsFreeze = true },
new FormTemplate { Id = 3, Active = false, Name = "last", IsFreeze = false }
}
};
return Json(donnees);
}
公共JsonResult FormTemplateGridData()
{
var donnees=新
{
总计=2,
第页=1,
记录=2,
行=新列表
{
新表单模板{Id=1,Active=true,Name=“first”,IsFreeze=true},
新表单模板{Id=2,Active=true,Name=“second”,IsFreeze=true},
新表单模板{Id=3,Active=false,Name=“last”,IsFreeze=false}
}
};
返回Json(donnees);
}
您知道这是一个异步对象,它返回一个jqXHR
对象,但不是您操作返回的值吗?你应该有一个属性,你发送到你的代码代替…你能解释我该怎么做吗?谢谢你的帮助!想想看,你可能根本不需要这个专栏。如果IsFreeze字段包含在来自服务器的数据中,则它可能也包含在rowobject参数中。我在我的网格中添加了.AddColumn(新列(“IsFreeze”).SetHidden(true))
,但在按钮中,如果(rowobject[“IsFreeze”])
不起作用,则函数不起作用。我在控制台中检查过,rowobject
是数组,它包含,例如,rowobject:Array[6]0:“Template1”1:“Group 2”2:“10.10.2010 0:00:00”3:“New”4:“False”5:“1”
,但我不知道如何从这个数组中获取第四个值。我尝试了rowobject[4]
,但它也不起作用。如果(rowobject[4]=“True”@Robin van der Knaap非常感谢,我已经找到了这种方法。@Heidel现在有一种解决方案可行吗?但是对于rowobject,它取决于您的FormTemplateGridData
操作返回的json对象。就我而言,rows
是FormTemplate
的列表,因此可以直接访问不同的字段因此,如果类型是boolean,我会得到一个jsbool(这就是为什么我的测试是rowobject[“IsFreeze”]
。。。
function buttonize(cellvalue, options, rowobject) {
var result = '<input type="button" value="Edit" onclick="editTemplate(' + options.rowId + ')">' + ' '
+ '<input type="button" value="Delete" onclick="deleteTemplate(' + options.rowId + ')">' + ' ';
if (rowobject["IsFreeze"]) {
result += '<input type="button" value="Activate" onclick="activateTemplate(' + options.rowId + ')">';
}
else {
result += '<input type="button" value="Deactivate" onclick="deactivateTemplate(' + options.rowId + ')">';
}
return result;
}
public JsonResult FormTemplateGridData()
{
var donnees = new
{
total = 2,
page = 1,
records = 2,
rows = new List<FormTemplate>
{
new FormTemplate { Id = 1, Active = true, Name = "first", IsFreeze = true },
new FormTemplate { Id = 2, Active = true, Name = "second", IsFreeze = true },
new FormTemplate { Id = 3, Active = false, Name = "last", IsFreeze = false }
}
};
return Json(donnees);
}