0

in asp.net core I made a service to add new row and another one to update row. I call the service from the controller based on JS fetch request. now I need from the service response to return partialView and some Json like that

var data = { htmlContent : '<html table row >', mode : 'insert', isSuccessful : true }

// then when I get response from the controller 

.then((data) => {
 jsonObj.parse(date);
 // ... some logic as follows

 if (jsonObj.mode == 'create')
 {  
   if (isSuccessful === 'true') { 
     // ... appendChild with [jsonObj.htmlContent]
   } else { 
     // ... show modal with [jsonObj.htmlContent]
   }
 } else if (jsonObj.mode == 'update') {
   if (isSuccessful === 'true') { 
     // ... insertInto (index) with [jsonObj.htmlContent]
   } else { 
     // ... show modal with [jsonObj.htmlContent]
   }
 }
 } else if (jsonObj.mode == 'delete') {
   if (isSuccessful === 'true') { 
     // ... remove element with temp message [jsonObj.htmlContent]
   } else { 
     // ... show modal with [jsonObj.htmlContent]
   }
 }
});

for the service I return response class like this

   // service
   public addNew()
   {
      try(){
      _dbContext.add(...
      var result = _dbContext.saveChanges();
      // here is the question
      // this is service not ActionResult
      // I need to return here html content out of partialView
      // along with some other Json data
      return new response{htmlContent: partialView(''), mode: 'insert', isSuccessful: true}; 
      }catch
      {
          return new response{htmlContent: '<h1>something went wrong !</h1>', mode: 'insert', isSuccessful: false}; 
      }
   }

   public class Response
   {
       public Dynamic htmlContent { get; set; }

       public bool isSuccessful { get; set; }

       public Crud mode { get; set; }

   }

   public enum Crud () {
     create, review, update, delete
   }

in the controller I return to js the addNew result which is Response class

   // after injection I use the service here 
   public IActionResult manage()
   {
     return _myService.addNew();
   }
 

finally and again is possible to return partialView along with some Json data from the microservice like shown above

  return new response{htmlContent: partialView(''), mode: 'insert', isSuccessful: true}; 

I think returning partialView is not allowed without IActionResult, however geeks absolutely know someway

1 Answers1

0

A PartialView can only be interpreted and resolved to HTML by a razor view.

In your case, you are calling an API directly from Javascript so you have no razor component to interpret a PartialView response.

You either need to roll with the framework and use razor view or use javascript (or jQuery) to create the html that you want.

Neil W
  • 7,670
  • 3
  • 28
  • 41