如何使用数据注释中的远程属性验证 Telerik mvc 网格? [英] how to validate telerik mvc grid using remote attribute in data annotations?
问题描述
我想使用 Telerik mvc grid(批量编辑)检查数据库中是否已经存在用户名.但问题是为了验证它,我需要在 CheckDuplicateType 函数中将 applconst_id 作为参数传递.但它总是返回未定义",我不知道如何获得正确的值.
这是我的模型类:
公共类 masterTypeCont{[钥匙]公共 Int32 applconst_id { 获取;放;}[Required(ErrorMessage = "这个字段需要一个值!")][远程(CheckDuplicateType",类型",AdditionalFields = "applconst_id",ErrorMessage = "代码已经存在.")]公共字符串 note1 { 获取;放;}}
这是我的控制器:
[HttpGet]公共虚拟 JsonResult CheckDuplicateType(masterTypeCont tipe, String applconst_id){Int32 intID = Convert.ToInt32(applconst_id);返回 Json(typeEnt.ValidateCustomer(intID, tipe.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();布尔值 = x == 0 ?真假;返回资源;}
这是我的观点:
@{ViewBag.Title = "类型列表";布局 = "../Shared/_Layout.cshtml";}<div class="direct-content">@using (Html.BeginForm()){@(Html.Telerik().Grid().Name("TypeGrid").Localizable("id-ID").ToolBar(命令 =>{命令.插入();命令.提交更改();}).DataKeys(keys => 键.Add(o => o.applconst_id).RouteKey("applconst_id")).DataBinding(dataBinding =>{数据绑定.Ajax().Select("_SelectAllType", "类型").Update("_SaveBatchType", "类型");}).Columns(columns =>{column.Bound(o => o.applconst_id).Hidden();column.Bound(o => o.note1).Title("Name");列.命令(命令=>{commands.Delete().ButtonType(GridButtonType.Text);}).title("命令");}).ClientEvents(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">}
我在那里写了隐藏输入来包含 applconst_id,但它的值仍然没有传递给控制器.
抱歉我的英语不好.
我们将不胜感激任何帮助.
谢谢.
感谢之前的回答.
经过一番研究,我终于找到了解决自己问题的方法.
我在那里创造了一个小技巧,也许这不是一个好方法,但至少它可以解决我的问题.
基本上,我只是使用元素的 name 属性,以便将其值传递给控制器.
在这里,我将详细解释我是如何做到这一点的.
首先,我将视图中的网格列更改为如下所示:
请关注 applconst_id 隐藏字段.我在那里添加了客户端模板.
columns.Bound(o => o.applconst_id).Hidden().ClientTemplate("<input class='typeID' name='common_id' value='<#= applconst_id #>'>");column.Bound(o => o.note1).Title("投诉性质");
其次,我在onEdit"函数中添加了这段代码:
//这个正在编辑的代码更改ID,以便可以传递给控制器var $inputID = $('.t-grid-edit-cell').prev().find('.typeID');$inputID.attr('name', 'applconst_id');//改回没有被编辑的ID,这样它就不会传递给控制器$('td:not(.t-grid-edit-cell)').prev().find('.typeID').attr('name','common_id');
第三,这是我的控制器:
[HttpGet]public JsonResult CheckDuplicateType(String applconst_id, String note1){Int32 IntID = Convert.ToInt32(applconst_id);返回 Json(typeEnt.ValidateCustomer(IntID, note1), JsonRequestBehavior.AllowGet);}
我的ValidateCustomer"函数保持不变.
我希望我的解决方案可以帮助其他和我有同样问题的人.
谢谢.
问候,
L.W.
I want to check if username already exists in database using telerik mvc grid (batch edit). But The problem is in order to validate it, I need to pass applconst_id as a parameter in CheckDuplicateType function. But it always return "undefined", I don't know how to get the right value.
this is my model class:
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; }
}
and this is my controller:
[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);
}
And this is ValidateCustomer function:
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 is my view:
@{
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>
I write hidden input there to contains the applconst_id, but still it value doesn't passed to controller.
Sorry for my bad english.
Any help will be deeply appreciated.
Thanks.
Thanks for the answers before.
After doing some research, I finally found solution for my own problem.
I create a little trick there, maybe this isn't a good way, but at least it can solve my problem.
Basically, I just play with the name attribute of an element so that it value can be passed to controller.
Here, I will explain in detail how I've done this.
First, I changed Grid columns in my View to be like this:
Please concern on applconst_id hidden field. I add client template there.
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");
Second, I add this code in "onEdit" function:
//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');
Third, this is my controller:
[HttpGet]
public JsonResult CheckDuplicateType(String applconst_id, String note1)
{
Int32 IntID = Convert.ToInt32(applconst_id);
return Json(typeEnt.ValidateCustomer(IntID, note1), JsonRequestBehavior.AllowGet);
}
And my "ValidateCustomer" function stay the same.
I hope my solution can help other people who have the same problem with me.
Thanks.
Regards,
L.W.
这篇关于如何使用数据注释中的远程属性验证 Telerik mvc 网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!