Help Understanding Remote Data Binding

2 posts, 1 answers
  1. Marion
    Marion avatar
    22 posts
    Member since:
    Nov 2016

    Posted 04 May Link to this post

    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

  2. Answer
    Alex Hajigeorgieva
    Admin
    Alex Hajigeorgieva avatar
    322 posts

    Posted 08 May Link to this post

    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 visualization (charts) and form elements.
Back to Top