This is a migrated thread and some comments may be shown as answers.

[Solved] Custom Validation Causes Edit Dialog to Close

2 Answers 130 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Andy
Top achievements
Rank 1
Andy asked on 28 Mar 2011, 04:55 PM
I am using version 2011.1.315.340 of the ASP.NET MVC grid on windows 7, visual studio 2010 and MVC 3, I am using server binding.  I want to perform an update on a record.  I have created a custom validation attribute for one of the properties on my view model.  The validation all works as expected.   However, even though the modelstate contains the error prior to my view being returned.  The popup edit dialog only renders a close button and nothing else. 

Could you tell me what I am doing wrong?  I've posted my code below which is hopefully sufficient for you to see the problem.  I should also point out that the insert works just fine.  The action method is identical to the update except for the code I call to perform my insert in to the db is different.

index.cshtml
@(Html.Telerik().Grid(Model)
    .Name("Users")
    .ToolBar(commands => commands.Insert())
    .DataKeys(keys => keys.Add(c => c.Id))
    .DataBinding(
        dataBinding => dataBinding.Server()
                .Select("Index", "User", new { mode = GridEditMode.PopUp, type = GridButtonType.Image })
                .Insert("Insert", "User", new { mode = GridEditMode.PopUp, type = GridButtonType.Image })
                .Update("Save", "User", new { mode = GridEditMode.PopUp, type = GridButtonType.Image })
                .Delete("Delete", "User", new { mode = GridEditMode.PopUp, type = GridButtonType.Image }))
    .Columns(columns =>
    {
        columns.Bound(o => o.Id).Width(20).Hidden();
        columns.Bound(o => o.FirstName).Width(100);
        columns.Command(
            commands =>
            {
                commands.Edit().ButtonType(GridButtonType.Image);
                commands.Delete().ButtonType(GridButtonType.Image);
            }).Width(200).Title("Commands");
    })
        .Editable(editing => editing.Mode(GridEditMode.PopUp))
        .Sortable(sorting => sorting.Enabled(true))
        .Groupable(grouping => grouping.Enabled(true))
        .Filterable(filtering => filtering.Enabled(true))
        .Pageable(paging => paging.Enabled(true))
)

User Controller
        [HttpPost]
        public virtual ActionResult Save(int id)
        {
            var entity = new User();
            if(TryUpdateModel(entity))
            {
                service.UpdateUser(id, entity);

                return RedirectToIndex();
            }
            var entities = service.GetAllUsersByCompanyId(1);
            return View("Index", entities);
        }

User.cshtml custom edit template
@model User

@Html.ValidationSummary()
     @Html.HiddenFor(model => model.Id)
    <table border="0">
    
    <tr>
    <td>
        @Html.LabelFor(model => model.FirstName)
    </td>
    <td>
        @Html.EditorFor(model => model.FirstName)<br/>
        @Html.ValidationMessage("FirstName")
    </td>
    </tr>
    
    <tr>
    <td>
        @Html.LabelFor(model => model.Surname)
    </td>
    <td>
        @Html.EditorFor(model => model.Surname)<br/>
        @Html.ValidationMessage("Surname")
    </td>
    </tr>

    </table>
   
User Model
    public class User
    {
        public int Id { get; set; }

        [Required]
        public string FirstName { get; set; }

        [Required]
        public string Surname { get; set; }

        [UniqueUsername(ErrorMessage = "Username must be unique")]
        public string Username { get; set; }

    
UniqueUsername Custom Attribute
    [AttributeUsage(AttributeTargets.Property)]
    public class UniqueUsernameAttribute : ValidationAttribute
    {
        public UniqueUsernameAttribute()
        {

        }

        public override Boolean IsValid(Object value)
        {
            var service = new AuthenticationService();
            return !service.UsernameExists(value as String);
        }
    }

Insert Action method
        [HttpPost]
        public virtual ActionResult Insert()
        {
            var entity = new User();
            if(TryUpdateModel(entity))
            {
                var currentUser = controllerMembershipService.GetMembershipUser();
                service.AddUser(entity, currentUser.Id, currentUser.CompanyId);

                return RedirectToIndex();
            }

           var entities = service.GetAllUsersByCompanyId(1);
            return View("Index", entities);
        }

2 Answers, 1 is accepted

Sort by
0
Andy
Top achievements
Rank 1
answered on 28 Mar 2011, 05:01 PM
This also does not work if I change the GridEditMode to be InForm
0
Jekin
Top achievements
Rank 1
answered on 23 Aug 2011, 06:21 AM
I have the same issue.The problem drove me crazy!! Is there anybody know how to fix the problem?
What about the you Telerik Team?
Tags
Grid
Asked by
Andy
Top achievements
Rank 1
Answers by
Andy
Top achievements
Rank 1
Jekin
Top achievements
Rank 1
Share this question
or