ASP.NET MVC – Partial Model Updates From View

ajaxasp.net-mvcmvc

I hope this is on-topic here since I'm a bit new to the whiteboard…

I'm currently developing an application using ASP.NET MVC 5 (and WebAPI 2). One of my domain entities is huge, and is stored in a document database (MongoDb in this case). The reason it's so large is that it resembles the fields on a paper form that used to be used for this process. It logically makes sense for this entity to be as large as it currently is. I'm currently brainstorming the best way to handle the updating of this entity. I'm currently using a 1:1 model of this entity.

I'm going to explain what I'm in the process of implementing below, but nothing is set in stone at this point.

The View currently is split up into sections of this large model where a user can click on a section and then the section then appears as a form using some JavaScript magic for the end-user to update those fields (properties of the model). This is where I'm not sure if I'm doing this properly: The current implementation is sending the ENTIRE model to a WebAPI controller via $.ajax() to update the database with these changes, and if successful I update the View on the client side to display the current values.

I feel like I'm not really leveraging the ASP.NET MVC framework here though except for the initial display of the view, since I'm performing these updates on the client side.

Is this the proper way to do partial updates of a model (at least using ASP.NET MVC 5)? I don't really want to have to refresh the entire page by requesting a new view after one of these partial submissions using the server-side processing of forms. Should each of these sections be a partial view along with smaller models that can then update the larger entity? In this case I believe that I can then utilize the server-side processing for forms since it can then send a new partial view? (Am I even understanding how to use partial views here?!)

I'm a bit confused if you couldn't tell…

Best Answer

Splitting your model into smaller models makes sense. You can use Ajax.BeginForm to submit the data of your partial view. So you can use your controller's action methods to call the service layer (your WEB API). In the action method you can create an object of your Main entity and populate it with your smaller model's values and send it to the API.