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

Grid Ajax Editing Mode: Popup - Custom Validate problem

10 Answers 178 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.
peter
Top achievements
Rank 1
peter asked on 16 Sep 2011, 10:24 AM
Now I'm working with telerik mvc Grid Editing - Ajax Editing - Edit mode: Pop-up with a Editor template

I want to custom validate in my model : but it doesn't work correctly. Any body can help me?

I have a sample in attach file for this case.
- ProductsViewModel inherit IValidatableObject
public class ProductsViewModel : IValidatableObject
    {
        [Required]
        public int ProductID { get; set; }

        [Required]
        [StringLength(80)]
        public string ProductName { get; set; }

        [Required]
        [DataType(DataType.Currency)]
        [Range(1, 100)]
        public decimal? UnitPrice { get; set; }

        public bool Discontinued { get; set; }

        [Required]
        [UIHint("Category")]
        [Display(Name = "Category")]
        public string CategoryID { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (ProductName == "Fire")
            {
                yield return new ValidationResult("Can not sell fire, too dangerous!!!");
            }
        }
    }

And I have a Editor template: ProductsViewModel.cshtml
@model ProductsViewModel
@Html.HiddenFor(p => p.ProductID)
<ul>
    <li>@Html.LabelFor(p => p.ProductName)
        @Html.EditorFor(p => p.ProductName) </li>
    <li>@Html.LabelFor(p => p.UnitPrice)
        @Html.EditorFor(p => p.UnitPrice)
    </li>
    <li>@Html.LabelFor(p => p.CategoryID)
        @Html.EditorFor(p => p.CategoryID)
    </li>
    <li>@Html.LabelFor(p => p.Discontinued)
        @Html.EditorFor(p => p.Discontinued)
    </li>
</ul>
@Html.ValidationSummary()





10 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 30 Sep 2011, 09:07 AM
Hi Peter,

I have looked at the provided project. It seems that the validation rules are set to the viewModel (used for grid's population), however in the update action the entity is used instead viewModel. Therefore, the custom validation is not executed as the different object is used during validation.

Regards,
Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
peter
Top achievements
Rank 1
answered on 30 Sep 2011, 09:25 AM
Hi Rosen,
Thanks for your answer
You gave me "the reason" the custom validation is not executed

my problem in my project is: 
I used a grid - ajax editing - popup mode
And I need check a unique field before update/insert data into database.
How I can do that? Can you give me a solution or a sample with the best?
0
Rosen
Telerik team
answered on 30 Sep 2011, 01:03 PM
Hello Peter,

You may use MVC remote validation feature or manually validate the entity state in the Action method before it is inserted/updated. As you may agree implementation of  domain validation is not directly related to our product.

All the best,
Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
peter
Top achievements
Rank 1
answered on 03 Oct 2011, 12:58 AM
Hi Rosen,

Thanks for your help,
Have a nice week.

I have already tried to use Remote validation before, it worked. But I always must click 'Submit' button twice in edit popup form to persist a valid data set into database: in the first click, if input data is valid -> nothing happens, after that with second click I can persist valid data set into database.

Until now, I still find a solution for this problem.

0
Rosen
Telerik team
answered on 03 Oct 2011, 07:40 AM
Hello Peter,

This issue has been already resolved. You may upgrade to latest Q2 2011 SP1 version and give it a try.

Regards,
Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
peter
Top achievements
Rank 1
answered on 04 Oct 2011, 01:05 AM
Thanks you very much
I will get latest release version and test it.
0
peter
Top achievements
Rank 1
answered on 04 Oct 2011, 03:07 AM
Hi Rosen,
I tried Q2 2011 SP1 version
This bug "Several clicks on Update button are required, in order changes to be submit if remote validation is used " is fixed
But It's not good as I expected because Remote attribute do not work.

You can see my following sample
0
Rosen
Telerik team
answered on 04 Oct 2011, 08:34 AM
Hello Peter,

Could you please elaborate a bit more on what is the behavior you are expecting, as maybe I'm missing something obvious. A step by step instructions how to recreated it will be appreciated.

All the best,
Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
peter
Top achievements
Rank 1
answered on 04 Oct 2011, 10:27 AM
Hi Rosen,

I had a mistake in my sample, therefore you couldn't understand me. I will re-test everything again and describes my expectation for details after that.

Facilities I also have a question look forward to you reply.

in product page: I have a Category DropDownList, when user chooses a item in dropdownlist then display all products of chosen category in the product grid (using ajax editing popup mode)
Products Grid:
Html.Telerik().Grid<ProductsViewModel>()
                .Name("Products")
                .DataKeys(dataKeys => dataKeys.Add(p => p.ProductID))
                .ToolBar(toolBar => toolBar.Insert())
                .Columns(columns =>
                {
                    columns.Bound(p => p.ProductName);
                    columns.Bound(p => p.CategoryID).Width(250).Title("Category");
                    columns.Bound(p => p.UnitPrice).Format("{0:c}").Width(80);
                    columns.Bound(p => p.Discontinued).Width(80)
                                        .ClientTemplate("<input type='checkbox' disabled='disabled' name='Discontinued' <#=Discontinued? checked='checked' : '' #> />");
                    columns.Command(commands =>
                    {
                        commands.Edit();
                        commands.Delete();
                    }).Width(200);
                })
                .DataBinding(dataBinding => dataBinding.Ajax().Select("Select", "Home")
                                                            .Update("Update", "Home")
                                                            .Insert("Insert", "Home")
                                                            .Delete("Delete", "Home")
                )
                .Editable(editable => editable.Mode(GridEditMode.PopUp))
                .Pageable()
                .Sortable()
EditorTemplate file:
@model ProductViewModel
@Html.ValidationSummary()
@Html.HiddenFor(p => p.ProductID)
@Html.HiddenFor(p => p.CategoryID)
<ul>
    <li>@Html.LabelFor(p => p.DisplayName)
        @Html.EditorFor(p => p.DisplayName)
    </li>
</ul>

User click "Create New", CategoryID hidden field alway is '0' (I think because a "new" ProductViewModel is generated to Editor Form).

==> The question: If I in use Insert() command in ToolBar, how I can set (pass, transfer) "chosen CategoryID" of Category DropDownList to "CategoryID" hidden field of Editor form when user click "Create New" button ?

Note: Currently, I must use a Session to store "the chosen category" and uses after that, I think it not good solution.

Hope that you can understand me ^-^
0
Rosen
Telerik team
answered on 05 Oct 2011, 08:34 AM
Hello Peter,

Default values for insert form could be set through DefaultDataItem method of Editable. If you want to set values dynamically you may use Edit client event.

All the best,
Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
Tags
Grid
Asked by
peter
Top achievements
Rank 1
Answers by
Rosen
Telerik team
peter
Top achievements
Rank 1
Share this question
or