Hello,
I was working on a project of SPA application with Web API, OData and DataJS.
I have started a fresh new project with the update to Web API2 and the
Javascript of the previous version.
I have problem to understand how the navigation links work.
I understood how to create a link but I don't get how to update a link or update a
collection of links.
Here's my model :
class Application { int Id string Name string Comment virtual ICollection<ApplicationDivision> ApplicationDivisions } class ApplicationDivision { int Id int ApplicationId int DivisionId virtual Application Application virtual Division Division } class Division { int Id string Name string Comment virtual ICollection<ApplicationDivision> ApplicationDivisions }
Following some tutorials, I have added this method to handle adding an
ApplicationDivision to an Application, I understand the POST verb but not how the PUT verb works...
[AcceptVerbs("POST", "PUT")] public async Task<IHttpActionResult> CreateLink([FromODataUri] int key, string navigationProperty, [FromBody] Uri link) { try { if (!ModelState.IsValid) { return BadRequest(ModelState); } Application parent = await db.Applications.FindAsync(key); if (parent == null) { return NotFound(); } switch (navigationProperty) { case "ApplicationDivisions": int childKey = GetKeyFromLinkUri<int>(link); ApplicationDivision child = new ApplicationDivision(); child.ApplicationId = key; child.DivisionId = childKey; parent.ApplicationDivisions.Add(child); await db.SaveChangesAsync(); return StatusCode(HttpStatusCode.NoContent); default: return NotFound(); } } catch (Exception ex) { throw ex; } }
I have tested this above code in Fiddler with the POST Request:
/odata/Applications(1)/$links/ApplicationDivisions
Content-Type: application/json
{"url":"http://localhost:51067/odata/Divisions(2)"}
So, I can add a Division to an Application but in my UI, I convert the collection of
ApplicationDivisions to a listbox of Divisions and users can do a multiple selection.
I want to update the collection of ApplicationDivisions with the array of now selected Divisions.
How can I do that ? Should I make one loop to delete the links that are
no longer valid and one loop to add new ones ? Should it be an array of links in the data part of the request ?
Should it be a Patch verb ?? Or is it my approach that is wrong ?
Thanks for any help,
Claude