angularjs - Angular Web API $http post exclude list -


i have web api (2) project has departments , employees. employee has department, , department has list of employees.

now in frontend, when creating or editing employee, user must select department. when posting api, department contains list of employees (which causes invalid modelstate), how can prevent this?

this relevant setup:

models:

public class employee : ientity, icreatedon, imodifiedon, imappable {     [key]     public virtual int id { get; set; }      public virtual department department { get; set; }      // .. other properties }  public class department : ientity, imappable {     [key]     public virtual int id { get; set; }      public virtual icollection<employee> employees { get; set; }      // .. other properties } 

web api controller:

public class employeescontroller : apicontroller {     private readonly iemployeeservice _employeeservice;      public employeescontroller(iemployeeservice employeeservice)     {         this._employeeservice = employeeservice;     }      // .. get, post, delete etc.      // put: api/employees/5     [responsetype(typeof(void))]     public ihttpactionresult putemployee(int id, employeevm employee)     {         // invalid, because employee has department, in turn has list of employees can invalid         // exclude list of employees validation, or better prevent being sent api         if (!modelstate.isvalid)         {             return badrequest(modelstate);         }          // update etc..          return statuscode(httpstatuscode.nocontent);     } 

angular (dataservice.js):

app.factory('dataservice',     ["$http",     function ($http) {          return {             // other functions             updateemployee: _updateemployee         }          function _updateemployee(employee) {              // maybe exclude list of employees in employee.department in here??             return $http.put(employeesurl + "/" + employee.id, employee);         }          // .. other functions     }]); 

notes:

  • it happens in both put , post (updating , creating)
  • i'm using automapper mapping viewmodels, same entities
  • i'm using entity framework orm

what i've tried:

  • [jsonignore] attribute on employees collection; causes employees not being loaded when loading department
  • [bind(exclude = "employees")] attribute in controller action parameters, did not have effect
  • [bind(exclude = "department.employees")] same

what works, i'm sure there must better solution:

function _updateemployee(employee) {      var newemp = angular.copy(employee);     delete newemp.department.employees;     return $http.put(employeesurl, newemp); } 

create new request updating employee. like:

public class updateemployeerequest{      public int employeeid {get;set;}      public int departmentid {get;set;}      //and on }   

for request can specify concrete validation.

and declare entities explicit id.:

public class employee : ientity, icreatedon, imodifiedon, imappable {     [key]     public int id { get; set; }      [foreignkey( "department" )]     public guid departmentid { get; set; }      [required]     public virtual department department { get; set; }     // .. other properties } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -