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

ID of updated record not coming through to the controller

3 Answers 90 Views
Grid
This is a migrated thread and some comments may be shown as answers.
AWL
Top achievements
Rank 1
AWL asked on 23 Dec 2012, 11:22 PM
Hello,

I have a grid setup and working with read (paging, sorting etc.) and create actions. I'm having trouble with the update. Basically, the ID does not come through with the rest of the request. I can see it in the browser's network tab, so it is being sent, but when the controller action is finally executed, it is missing from the parameter object.

View Index.cshtml
@using Kendo.Mvc.UI
@using Mvc.Models
 
@model IEnumerable<ConciergeModel>
 
@{
    ViewBag.Title = "Manage Live-Connect Concierge List";
    Layout = "~/Views/Shared/_Kendo.cshtml";
}
 
<h2>@ViewBag.Title</h2>
 
<h3>Server Side Initialization</h3>
 
@{
    try
    {
    @(Html.Kendo().Grid(Model)
    .Name("Concierge")
    .Columns(columns =>
    {
        columns.Bound(c => c.ConciergeId).Groupable(false).Width(40);
        columns.Bound(c => c.ConciergeName).Width(240);
        columns.Bound(c => c.ConciergeEmail).Width(480);
        columns.Command(command => command.Edit());
    })
    .ToolBar(toolbar =>
    {
        toolbar.Create();
    })
    .Editable(editable => editable.Mode(GridEditMode.Inline))
    .Pageable()
    .Sortable()
    .Scrollable()
    .DataSource( dataSource => dataSource
        .Ajax()
        .Model(model => model.Id(c => c.ConciergeId))
        .Create(create => create.Action("ConciergeCreate", "Concierge"))
        .Read(read => read.Action("ConciergeRead", "Concierge"))
        .Update(update => update.Action("ConciergeUpdate", "Concierge"))
    ))
    }
    catch(Exception e)
    {
        @Html.Raw("<pre>" + e + "</pre>");
    }
}
ConciergeModel.cs
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
 
namespace Mvc.Models
{
    public class ConciergeModel
    {
        public ConciergeModel() { }
 
        [Required]
        [DisplayName("ID")]
        public long ConciergeId { get; set; }

 
        [Required]
        [DisplayName("Name")]
        [DataType(DataType.Text)]
        public string ConciergeName { get; set; }
 
        [Required]
        [DisplayName("Email Address")]
        [DataType(DataType.EmailAddress)]
        public string ConciergeEmail { get; set; }
 
    }
}
And the controller action in ConciergeController.cs
// POST: /mvc/{admin-id}/Concierge/ConciergeUpdate
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult ConciergeUpdate([DataSourceRequest] DataSourceRequest request,
    ConciergeModel concierge)
{
    if (concierge != null && ModelState.IsValid)
    {
        // At this point, concierge.ConciergeId is zero
        concierge.Update(_lccConciergeService);
    }
 
    return Json(new[] { concierge }.ToDataSourceResult(request, ModelState));
}
Here is an example request, taken from Firebug (FF 17.0)

Response Headers
Cache-Control private
Content-Length 0
Date Sun, 23 Dec 2012 23:19:15 GMT
Persistent-Auth true
Server Microsoft-IIS/8.0
X-AspNet-Version 4.0.30319
X-AspNetMvc-Version 3.0
X-Powered-By ASP.NET
X-SourceFiles =?UTF-8?B?QzpcTGVhZE1hc3RlclxMYXRlc3RccGxhdGZvcm1cTGVhZE1hc3RlclBsYXRmb3JtXEF3bC5MZWFkTWFzdGVyLk12Y1wxMjNcQ29uY2llcmdlc1xDb25jaWVyZ2VVcGRhdGU=?=
Request Headersview source
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control no-cache
Connection keep-alive
Content-Length 110
Content-Type application/x-www-form-urlencoded; charset=UTF-8
Cookie ASP.NET_SessionId=4ff34okn0jkkwqi3k0qswb4l
Host localhost:7171
Pragma no-cache
Referer http://localhost:7171/mvc/123/Concierges/
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0
X-Requested-With XMLHttpRequest

Parametersapplication/x-www-form-urlencoded
ConciergeEmail jimbob@allwebleadstransferportal.com
ConciergeId 2
ConciergeName Jim Bob
filter
group
sort
Source
sort=&group=&filter=&ConciergeId=2&ConciergeName=Jim+Bob&ConciergeEmail=jimbob%40allwebleadstransferportal.com

So you can see that the ID should be 2, but it is zero, which causes the update operation to fail - the dataservice can't run the update query without the ID.

At one point, I had the ID with a private setter and marked with a [ReadOnly(true)] attribute, but as you can see, I removed that and the problem persisted.

3 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 25 Dec 2012, 01:51 PM
Hello Eric,

Indeed your code looks valid and the MVC model binder should populate that field on the server. Can you assemble a project which we can run and investigate what's the reason for that behavior?

Thank you in advance for the cooperation.

Kind regards,
Petur Subev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
AWL
Top achievements
Rank 1
answered on 31 Dec 2012, 03:52 PM
As I was packaging my project up to send to you guys, I tried a few more things and it started working. I'm not sure which change did the trick, if you could perhaps illuminate me, that would be much appreciated.

In Index.cshtml, I changed the Datasource model to the following:
.Model(model => {
    model.Id(c => c.ConciergeId);
    model.Field(c => c.ConciergeId).Editable(false);
})
In the Model.cs class, I removed the [Required] and [DisplayName] attributes from the ConciergeId field (which was the field that wasn't coming through).

And in the controller... Hmm I didn't really add anything, just a try/catch block around the update method. I added that when I was having some unrelated database connectivity problems. To be clear, the problem was already manifest before the update method was called.

Any Ideas?
0
Petur Subev
Telerik team
answered on 01 Jan 2013, 12:03 PM
Hello Eric,

I am still not sure what could the the reason for this. A known issue is when the value is passed once through the form data and once through the query string (or as route parameter) and the value in the form data is empty string - however I do not think this is the case.

Kind regards,
Petur Subev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
AWL
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
AWL
Top achievements
Rank 1
Share this question
or