[RESOLVED]MVC pattern with Json Results

I have a design pattern question for JsonResults.

In my views I have Ajax calls to return Json results.

In the controller, it is a simple JsonResult function that returns the results.

JsonResult GetSuburbs(){

return JsonResult( etc….)

}

Since GetSuburbs will need to be called from several places to populate dropdownlists.

Do you usually place the GetSuburbs function within the SuburbController? or have a separate controller that holds dropdownlist functions?

anyone?

hi,

No need to create separate controller to hold json functionality. usually use inside any of the controller, you can call any time through jQuery AJAX script.

$.ajax({
        url: "Controller/GetSuburbs",
        type: 'POST',
        contentType: 'application/json',
        data: "",
        success: function () {           
        }
    });

[HttpPost]
public JsonResult GetSuburbs(string id)
{
// business logic.... }

 You can call any view page to communicate this Json action method….

Thanks,

Jai.

alphabeatsco

Since GetSuburbs will need to be called from several places to populate dropdownlists.

Do you usually place the GetSuburbs function within the SuburbController? or have a separate controller that holds dropdownlist functions?

In my opinion,   better having separate controller for all the dropdows whatever accessible to all kind of users. 

I know it can be called from any controller.
But I just think congregating all the dropdowblist methods in a separate will be a cleaner approach?

Any thoughts? Or its purely subjective?

I meant if we put getsuburb action in Suburbscontroller the controller may not accessible all kind of users.

If we place ddl actions in subjective controller . u need to maintain authorize at all the actions from many controllers.

instead, I maintain all ddl actions in controller and it is allowed to all roles.

Share if you have any other considerations.

You don’t have to put all of your code in controllers.  If you have a method you want to use from multiple controllers then put it in a normal class outside the controllers folder, and have your controllers create an instance of the class and call the method
that returns the data.

AidyF

You don’t have to put all of your code in controllers.  If you have a method you want to use from multiple controllers then put it in a normal class outside the controllers folder, and have your controllers create an instance of the class and call the method
that returns the data.

Yes, This is one option, enough to have static class

Leave a Reply