We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

AspNet Core ODataV4Adapter entity with string key

I have been working with the Aspnet Core 2.2 EJ2 Grid the grid is using the ODataV4Adapter, my entity has a primary key that is a string version of a guid. I am trying to bind it toe the following function signature:

public IActionResult Get([FromODataUri] string key) { }

My problem is that the adapter sends any request that requires the key as:
https://localhost:44355/odata/applicationusers(00132dc2-59e6-4f6d-949d-c38e5366b445)

Aspnet Core 2.2 OData will not bind this value to the key parameter,

It will bind, if the key is enclosed in single quotes: 
https://localhost:44355/odata/applicationusers('00132dc2-59e6-4f6d-949d-c38e5366b445')

Aspnet Core OData requires the single quote marks to bind. I have tried many things on the backend such as custom binders and custom OdataRoutingConventions, none work.

It is not clear to me how to force the ODataV4Adapter to enclose the key in single quotes, for post, patch and put requests. Nor can I see how I can trap the request before it is sent and wrap the key in quotes on the client side. Nor can I find an event that fires that I would be able to leverage to do this.

Thanks,

Scott 
  

1 Reply

PS Pavithra Subramaniyam Syncfusion Team May 29, 2019 10:43 AM UTC

Hi Scott, 
 
Sorry for inconvenience caused, 
 
We have validated your query and we suspect that you have expecting single quote query(guid value in single quotes which is primary key field) property with POST, PUT, Patch actions in ODataV4Adaptor. But, for OdataV4 we need to send the request without quotes and which is working fine at our end. We suggest you to refer the below code example and reference for more information. 
 
[index.cshtml] 
 
<ejs-grid id="Grid" allowPaging="true" allowSorting="true" allowFiltering="true" toolbar="@(new List<string>() { "Add","Delete","Update","Cancel" })">         
        <e-grid-editSettings allowAdding="true" allowEditing="true" allowDeleting="true" mode="Normal"></e-grid-editSettings>       
        <e-data-manager url="/odata/Books" adaptor="ODataV4Adaptor"></e-data-manager> 
        <e-grid-columns> 
            <e-grid-column field="guid" headerText="GuID" isPrimaryKey="true" width="150"></e-grid-column> 
            .   .   .   . 
       </e-grid-columns> 
    </ejs-grid> 
 
[BookController.cs] 
 
// GET api/values 
        [HttpGet] 
        [EnableQuery] 
        [System.Web.Http.AllowAnonymous] 
        public IQueryable<Book> Get() 
 
        { 
            return _db.Books; 
        }  
 
        // POST api/values 
        [HttpPost] 
        [EnableQuery] 
        [System.Web.Http.AllowAnonymous] 
        public IActionResult Post([FromBody]Book book) 
        { 
            _db.Books.Add(book); 
            _db.SaveChanges(); 
            return Created(book); 
        } 
 
        // PUT api/values/"5" 
        [HttpPut("{id}")] 
        [Route("api/[controller]")] 
        [System.Web.Http.AllowAnonymous] 
        public async Task<Book> Put(string id, [FromBody] Book book) 
        { 
            var entity = await _db.Books.FindAsync(book.guid); 
            _db.Entry(entity).CurrentValues.SetValues(book); 
            await _db.SaveChangesAsync(); 
            return book; 
        } 
 
        // DELETE api/values/5 
        [HttpDelete("{id}")] 
        [System.Web.Http.AllowAnonymous] 
        public async Task<int> Delete(int key) 
        { 
            var od = await _db.Books.FindAsync(key); 
 
            _db.Books.Remove(od); 
            await _db.SaveChangesAsync(); 
            return key; 
        } 
 
 
 
 
 
[Book.cs] 
 
public class Book 
    { 
        [Key] 
        // Please ensure the Guid field type declaration 
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
        public Guid guid { get; set; } 
        public int Id { get; set; } 
        public string ISBN { get; set; } 
        public string Title { get; set; } 
        public string Author { get; set; } 
        public decimal Price { get; set; } 
    } 
 
 
 
 
 
Regards, 
Pavithra S. 


Loader.
Live Chat Icon For mobile
Up arrow icon