How to validate telerik mvc grid using a remote attribute in data annotations?

advertisements

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.


Try like this:

[HttpGet]
public virtual ActionResult CheckDuplicateType(masterTypeCont tipe)
{
    var result = typeEnt.ValidateCustomer(tipe.applconst_id, tipe.note1);
    return Json(result, JsonRequestBehavior.AllowGet);
}