Asp.net mvc 3 如何在数据注释中使用远程属性验证telerik mvc网格?
我想使用telerik mvc网格(批量编辑)检查数据库中是否已经存在用户名。但问题是为了验证它,我需要在CheckDuplicateType函数中将applconst_id作为参数传递。但它总是返回“未定义”,我不知道如何获得正确的值 这是我的模型课:Asp.net mvc 3 如何在数据注释中使用远程属性验证telerik mvc网格?,asp.net-mvc-3,validation,asp.net-ajax,data-annotations,telerik-grid,Asp.net Mvc 3,Validation,Asp.net Ajax,Data Annotations,Telerik Grid,我想使用telerik mvc网格(批量编辑)检查数据库中是否已经存在用户名。但问题是为了验证它,我需要在CheckDuplicateType函数中将applconst_id作为参数传递。但它总是返回“未定义”,我不知道如何获得正确的值 这是我的模型课: public class masterTypeCont { [Key] public Int32 applconst_id { get; set; } [Required(ErrorMessage = "This fi
public class masterTypeCont
{
[Key]
public Int32 applconst_id { get; set; }
[Required(ErrorMessage = "This field needs a value!")]
[Remote("CheckDuplicateType",
"Type",
AdditionalFields = "applconst_id",
ErrorMessage = "Code already exists.")]
public String note1 { get; set; }
}
这是我的控制器:
[HttpGet]
public virtual JsonResult CheckDuplicateType(masterTypeCont tipe, String applconst_id)
{
Int32 intID = Convert.ToInt32(applconst_id);
return Json(typeEnt.ValidateCustomer(intID, tipe.note1), JsonRequestBehavior.AllowGet);
}
[HttpGet]
public JsonResult CheckDuplicateType(String applconst_id, String note1)
{
Int32 IntID = Convert.ToInt32(applconst_id);
return Json(typeEnt.ValidateCustomer(IntID, note1), JsonRequestBehavior.AllowGet);
}
这是ValidateCustomer函数:
public bool ValidateCustomer(Int32 id, String nama) {
Int32 x = db.appl_const.Where(a =>
a.group_id == "CH_COMP_CODE" &&
a.note1.Trim() == nama.Trim() &&
a.applconst_id != id).Count();
Boolean res = x == 0 ? true : false;
return res;
}
//this code change ID that is being edited, so that it can be passed to controller
var $inputID = $('.t-grid-edit-cell').prev().find('.typeID');
$inputID.attr('name', 'applconst_id');
//change back ID that is not being edited, so that it's not passed to controller
$('td:not(.t-grid-edit-cell)').prev().find('.typeID').attr('name','common_id');
我的看法是:
@{
ViewBag.Title = "Type List";
Layout = "../Shared/_Layout.cshtml";
}
<div class="direct-content">
@using (Html.BeginForm())
{
@(Html.Telerik().Grid<ComplaintHandling.Models.masterTypeCont>()
.Name("TypeGrid")
.Localizable("id-ID")
.ToolBar(commands =>
{
commands.Insert();
commands.SubmitChanges();
})
.DataKeys(keys => keys
.Add(o => o.applconst_id)
.RouteKey("applconst_id"))
.DataBinding(dataBinding =>
{
dataBinding.Ajax()
.Select("_SelectAllType", "Type")
.Update("_SaveBatchType", "Type");
})
.Columns(columns =>
{
columns.Bound(o => o.applconst_id).Hidden();
columns.Bound(o => o.note1).Title("Name");
columns.Command(commands =>
{
commands.Delete().ButtonType(GridButtonType.Text);
}).Title("Command");
})
.ClientEvents(events => events
.OnEdit("OnEditGrid")
)
.Editable(editing => editing.Mode(GridEditMode.InCell))
.Selectable()
.Pageable(pager => pager.PageSize(15))
.Filterable()
.Sortable()
.Scrollable(x => x.Height("450px"))
.KeyboardNavigation()
.Resizable(x => x.Columns(true))
)
<input type="hidden" name="applconst_id" id="applconst_id" value="1">
}
</div>
@{
ViewBag.Title=“类型列表”;
Layout=“../Shared/_Layout.cshtml”;
}
@使用(Html.BeginForm())
{
@(Html.Telerik().Grid())
.Name(“TypeGrid”)
.Localizable(“id”)
.工具栏(命令=>
{
Insert()命令;
commands.SubmitChanges();
})
.DataKeys(keys=>keys
.Add(o=>o.applconst_id)
.RouteKey(“applconst_id”))
.DataBinding(数据绑定=>
{
Ajax()
.选择(“\u SelectAllType”,“Type”)
.Update(“_SaveBatchType”,“Type”);
})
.列(列=>
{
columns.Bound(o=>o.applconst_id).Hidden();
columns.Bound(o=>o.note1).Title(“Name”);
columns.Command(commands=>
{
commands.Delete().ButtonType(GridButtonType.Text);
}).头衔(“命令”);
})
.ClientEvents(事件=>事件
.OnEdit(“OnEditGrid”)
)
.Editable(editing=>editing.Mode(GridEditMode.InCell))
.可选()
.Pageable(pager=>pager.PageSize(15))
.可过滤()
.Sortable()
.可滚动(x=>x.高度(“450px”))
.键盘导航()
.可调整大小(x=>x.Columns(true))
)
}
我在那里写入隐藏输入,以包含applconst_id,但它的值仍然没有传递给控制器
对不起,我的英语不好。我们将非常感谢您的帮助。
谢谢。像这样试试:
[HttpGet]
public virtual ActionResult CheckDuplicateType(masterTypeCont tipe)
{
var result = typeEnt.ValidateCustomer(tipe.applconst_id, tipe.note1);
return Json(result, JsonRequestBehavior.AllowGet);
}
谢谢你之前的回答 在做了一些研究之后,我终于找到了解决我自己问题的办法。
我在那里创造了一个小把戏,也许这不是一个好方法,但至少它可以解决我的问题。
基本上,我只是处理元素的name属性,以便将其值传递给控制器 在这里,我将详细解释我是如何做到这一点的 首先,我将视图中的网格列更改为:
请关注applconst_id隐藏字段。我在那里添加了客户端模板
columns.Bound(o => o.applconst_id).Hidden()
.ClientTemplate("<input class='typeID' name='common_id' value='<#= applconst_id #>'>");
columns.Bound(o => o.note1).Title("Nature of complaint");
第三,这是我的控制器:
[HttpGet]
public virtual JsonResult CheckDuplicateType(masterTypeCont tipe, String applconst_id)
{
Int32 intID = Convert.ToInt32(applconst_id);
return Json(typeEnt.ValidateCustomer(intID, tipe.note1), JsonRequestBehavior.AllowGet);
}
[HttpGet]
public JsonResult CheckDuplicateType(String applconst_id, String note1)
{
Int32 IntID = Convert.ToInt32(applconst_id);
return Json(typeEnt.ValidateCustomer(IntID, note1), JsonRequestBehavior.AllowGet);
}
我的“ValidateCustomer”函数保持不变
我希望我的解决方案能帮助其他和我有同样问题的人
谢谢
问候,,
L.W.我已经尝试过了,但是tipe.applconst\u id始终为0。还有其他解决办法吗?谢谢