0

I've been developing .NET projects for the past 3 years, and on my recent web .NET projects, I've been using Partial Views for all the screens that contain Forms for Add/Update/Delete or other information (e.g. User Info).

Here's a sample (not the exact code) of how I designed it.

For example on editing a user:

HTML - (A table that contains information about users)

<table style="width:100%">
  <tr>
    <th>ID</th>
    <th>Name</th> 
    <th>Edit</th>
  </tr>
   <tr>
   <td>94</td>
    <td>Jackson</td>
    <td><button type="button" onclick="LoadHtmlContent('94')"> Edit </button></td>
  </tr>
</table>

Javascript (Function that is executed when the user clicks the Edit button on the table)

function LoadHtmlContent(id)
{
   var id = id; // Get the id from a table row 
   var url = $("#EditUrl").val(); //Get the URL from a hidden input
   $.ajax({
   url: url,
   type: "GET"
   data: data, //optional
   success: function(response){
     if(response.success=true){
       $("#modal").html(response.content);
       $("#modal").show();
    }
     else{
       alert(response.message)
    }
   }
  )};
}

Controller:

public ActionResult LoadEditableScreen(int id)
{
   //Perform Security/Authorization checks here
  if(!Authorized)
  {
      return Json(new {success=false, message="Access Denied"});
  }
   var model = new ViewModel();
   //Load data from database layer here
   var partialView = CreatePartialView("_editScreen", model) //helper function 
   return Json(new {success=true, content = partialView });
   
}

Is this a good practice to use in my future projects as well? I'm seeing this not only from my perspective as developer, but from the user's as well. Because as a user its much easier for me to have everything I need on a single page without having to navigate through many pages. I've created this in a way that is dynamic and it can fit into all my pages and any type of data.

Thank you :)

1
  • I use a similar technique in a single-page application. Commented Aug 11, 2021 at 12:57

1 Answer 1

1

What you have here is a precursor variant to the single page application. There's nothing wrong with that. This style of development is precisely why MVC is able to return partial views as a response.

If you like this style of development, look into single page application (SPA) development. While this shifts the development from MVC/Razor to pure frontend (JS/TS), it further enhances the user experience that you get from this kind of design.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.