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

Help Understanding Remote Data Binding

1 Answer 67 Views
TreeList
This is a migrated thread and some comments may be shown as answers.
Marion
Top achievements
Rank 1
Marion asked on 04 May 2017, 02:34 PM

I am seeking help to understand the documentation found here:

http://demos.telerik.com/aspnet-mvc/treelist/remote-data-binding

My questions are:

In the following snip it 

public JsonResult Create([DataSourceRequest] DataSourceRequest request, EmployeeDirectoryModel employee)

{

if (ModelState.IsValid)

{

employeeDirectory.Insert(employee, ModelState);

}

return Json(new[] { employee }.ToTreeDataSourceResult(request, ModelState));

}

In this example the purpose is to create (insert) a new record. To do this this function calls the Insert method of the employeeDirectory method (which is presumable a method of the EmployeeDirectoryModel and it takes two parameters the employee (which was passed into this calling function, and the ModelState.

My question is, what does this Insert method do with the ModelState? It is passed in for a reason, so what is that reason?

The employee parameter will contain the data to be written to the database, except the primary key, does the Insert method load the primary key value into the model so that when it gets returned back (last line of snip it), there will be a fully loaded employee object?

When, where, how, does the ModelState get updated so that it knows that the employee object is full validated (i.e. has a primary key)?

Thank you

1 Answer, 1 is accepted

Sort by
0
Accepted
Alex Hajigeorgieva
Telerik team
answered on 08 May 2017, 12:04 PM
Hi Marion,

Please find the answers to you your questions below:

- What does this Insert method do with the ModelState? It is passed in for a reason, so what is that reason?

The ModelState.IsValid provides s a way to check if the model is valid and then insert it in the database. If there are any invalid or missing properties in the passed model based on the data annotations added to the properties of the model, the model will not be inserted in the database.

- Does the Insert method load the primary key value into the model so that when it gets returned back (last line of snip it), there will be a fully loaded employee object?

Yes it does, here is the code from the EmployeeDirectory Service:
{
  var first = GetAll().OrderByDescending(e => e.EmployeeId).FirstOrDefault();
  var id = (first != null) ? first.EmployeeId : 0;
 
  employee.EmployeeId = id + 1;
 
  GetAll().Insert(0, employee);
 }
 else
 {
  var entity = employee.ToEntity();
 
  db.EmployeeDirectory.Add(entity);
  db.SaveChanges();
 
  employee.EmployeeId = entity.EmployeeID;
 }

- When, where, how, does the ModelState get updated so that it knows that the employee object is fully validated (i.e. has a primary key)?

Here is a blog post I found on the internet which seems to cover the process of how the ModelStateDictionary in MVC gets updated:

https://www.exceptionnotfound.net/asp-net-mvc-demystified-modelstate/

MSDN references:

ModelState class -  https://msdn.microsoft.com/en-us/library/system.web.mvc.modelstate(v=vs.118).aspx
ModelStateDictionary class - https://msdn.microsoft.com/en-us/library/system.web.mvc.modelstatedictionary(v=vs.118).aspx

Regards,
Alex Hajigeorgieva
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data (charts) and form elements.
Tags
TreeList
Asked by
Marion
Top achievements
Rank 1
Answers by
Alex Hajigeorgieva
Telerik team
Share this question
or