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

Insert with Popup template

3 Answers 455 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 14 Aug 2012, 05:07 PM
I'm using the popup editing mode with a custom MVC template for editing, having just created an EditorTemplate with the name of my model. Within this template I have two DropDownLists, both bound using a similar method; each have a custom template and these are specified by way of UIHint metadata on my model properties. All this works fine for edits; the DDL values are posted back correctly, and I even see the background grid change dynamically as I change the values before doing the Update.

However, inserts don't work. The same edit template appears and the text fields update the background grid dynamically, but the DDLs don't. When posted back, the ID values don't have the ID values from the DDLs, but the text values. This of course causes validation to fail.

Edit: Sample project attached. Ignore the fact that the two DDL values don't show in the grid initially; they do in the full solution so I've just missed something here. But if you edit an item and select values from the two DDLs, that's fine. Not so with the Create.

Side question: when using the same template for Edit/Create can we change the title of the popup window?


Edit 2: Ok, more investigation this morning, after hacking a workaround. It looks like the request form variables come back different between Edit and Create, even though they use the same editor. I'm currently seeing the following in Request.Form:

DefaultWorkPoolId.Id
DefaultWorkPoolId.Name
DefaultWorkPoolId[Id]
DefaultWorkPoolId[Name]

Clearly the model binding can't bind these to the property, which is DefaultWorkPoolId, but the question is why the Create behaviour is different. It would be good to get an answer on this so I can remove the hacked code; this is only the first screenof many and I don't want to have to repeat this everywhere.

3 Answers, 1 is accepted

Sort by
0
Accepted
Nikolay Rusev
Telerik team
answered on 15 Aug 2012, 02:54 PM
Hello Dave,

Recently we've made some changes on how Guid fields are treated. Those changes are available in latest internal builds after Q2 2012. In order to make your scenario working you will need some of those build.

Few more things that should be changed:
 - the create action method should look as follow:
[HttpPost]
        public ActionResult Create([DataSourceRequest] DataSourceRequest request, TaskModuleDto module)
        {
            if (module != null && ModelState.IsValid)
            {
                SetViewDataWithComponentsAndWorkPools();
                TaskRepository.Create(module);
            }
 
            //return Json(ModelState.ToDataSourceResult());
 
            return Json(new[] { module }.ToDataSourceResult(request, ModelState));
        }

For more details check this article: http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/ajax-editing

 - Id field should define default value, otherwise the model binder will report model state error, because the Id will be send as empty value. So you can change the model definition as follow:
.Model(model =>
                   {
                       model.Id(p => p.Id);
                       model.Field(p => p.Id).DefaultValue(new Guid());
                       model.Field(p => p.Name);
                       ...
                   })

 - you will also need to generated valid guid for the Id field for new records. In the app this means the following modification in TaskRepository.cs :
public static void Create(TaskModuleDto module)
        {
            var modules = HttpContext.Current.Session["Modules"] as List<TaskModuleDto>;
 
            if (modules == null)
                modules = new List<TaskModuleDto>();
 
            module.Id = Guid.NewGuid();
            modules.Add(module);
        }


Regards,
Nikolay Rusev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Dave
Top achievements
Rank 1
answered on 15 Aug 2012, 04:00 PM
Awesome, thank you Nikolay. It was the default Guid settings that were killing me. The errors in Create I'd have solved since already I'd bookmarked that link, but just hasn't connected the problems I was having with default values.

I'll just apply this to my main solution.

Thanks for the help.

Dave
0
jonathan
Top achievements
Rank 1
answered on 19 Nov 2012, 07:37 PM
Hello,

This put me on the right track but I still am having isues.  Everything builds, but when I hit the page it says it cannot convert GUID to string?

Here is my scenerio:

cshtml:
    @(Html.Kendo().Grid<ReturnItemThin>()
            .Name("OrderPackingSearchGrid")
            .Columns(columns =>
            {
                columns.Bound(c => c.Id).Title("GUID");
                columns.Template(@<text></text>).ClientTemplate("<input type='checkbox' #= IsSelected ? checked='checked':'' # class='chkbx' />").Width("45px")
                .HeaderTemplate("<span><input type='checkbox' id='masterCheckBox' onclick='checkAll(this)'/>Select All</span>");
                               columns.Command(command => { command.Edit().Text("Edit"); }).Hidden(true);
            })
            .Editable(e => e.Mode(GridEditMode.InLine))
            .DataSource(dataSource => dataSource
                .Ajax()
                .Model(model =>
                {
                    model.Id(p => p.Id);
                    model.Field(p => p.Id).DefaultValue(new Guid());
                })
                .PageSize(9)
                .Read(read => read.Action("SearchByOrderPackingSlipId", "ItemSearch")
                    .Data("OrderPackingFormValues"))
                .Update(update => update.Action("UpdateReturnItem", "Return"))
                .ServerOperation(false)
            )
            .Pageable()
            .Sortable()
)



Model:
    [DataContract]
    public class ReturnItemThin
    {
        [DataMember]
        public Guid Id { get; set; }
        [DataMember]
        public bool IsSelected { get; set; }
}
 public ReturnItemThin(EquipmentReturn.DataModel.ReturnItem returnItem)
        {
            Id = Guid.NewGuid();
 IsSelected = false;

}
Tags
Grid
Asked by
Dave
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Dave
Top achievements
Rank 1
jonathan
Top achievements
Rank 1
Share this question
or