2

I am working on ASP.NET MVC 4 Project of our Company. It is revenue based application and It has some Filters at every Controller and Action of the application.They are Year and Quarter like some filters. for these filter changes I used Create Base Model, (It contains these filter values) and every other Models inherit from this Base Model.Base Model is use at @layout view. Every change of filter should update the view. Base Model in layout view something like this

public class BaseModel
{
 public string YearSelected{ get; set;}
 public string QuarterSelected{ get; set;}
}

and other Models are inherit from this BaseModel

public class RevenueModel:BaseModel
{
 // other stuff
}


For all these things I am sending through the parameters.Seems like now Parameters are increase requirements are changes to add more parameters
1.I want to know the method that handle is correct and If method is insufficient Suggest me best way to handle this parameter changes.

2.In the UI(View),
When user change the view by checking radio button and click on apply filter button I am using jquery for handle this,

window.href='Url Action with new Parameters';
window.href='@Url.Action("SomeAction","Controller",new{ // those all parameters } ';

When button click window.href will automatically call and update the view I want to know
Is this method Robust? Suggest me best way to handle this scenario.

"Simply I need a answer for if i use jquery to call an action and use high numbers of parameters for the call controller action"

unique
  • 5,442
  • 6
  • 23
  • 26
  • I'm not really clear on #1. Are you asking if it's ok to add more properties to your model when you need to pass in more parameters? – Josh Noe Oct 13 '13 at 15:39
  • @JoshNoe Yes, now it has about 10 parameters – unique Oct 13 '13 at 16:12
  • Howmany params are we talking about here? If I understand your question right, then you should be doing a POST on radio button select and refresh the page. In the post you can pass a model as one single parameter and you don't have to worry about multiple parameters here – HaBo Oct 13 '13 at 17:20
  • @HaBo there are many all about 10 filters they are in different div in views. what do you think about that window.href for the refresh problem – unique Oct 13 '13 at 18:29
  • @unique that should be fine as long as it is serving your purpose. – HaBo Oct 13 '13 at 18:53
  • Just be aware that IE has length limits for the address (1024 chars?), so it's possible that lengthy query strings may not make it back to the server, either in the first post or subsequent ones. – ps2goat Oct 13 '13 at 19:49
  • @ps2goat I want to know is it bad practice pass parameters instead of pass whole model? – unique Oct 14 '13 at 04:28
  • No, it is not bad practice, but there are different situations where one way works better than others. In individual cases, one could make an argument that it would be bad practice. However, I would argue that the GET query string could get cut off at a certain point, which would make relying on the query string with so many parameters a bad practice. If you're sure the length of the query string + url will never be too long, then it will work fine. – ps2goat Oct 14 '13 at 16:32
  • 1
    @ps2goat - its 2083 characters in IE :) http://stackoverflow.com/questions/812925/what-is-the-maximum-possible-length-of-a-query-string – Tommy Feb 10 '14 at 15:23

1 Answers1

3

What you're doing is doable, but as @ps2goat points out, you can run into issues if you have too many GET parameters in the URL.

But the right answer depends on how the filters will be used. Will the user often change the filters or will he rarely change them? If the user is expected to browse through your app and only rarely change the filters, I would suggest you to use another approach than including the parameters as GET parameters in the URL because:

  1. You could run into problems if the total length of your URL becomes too long, as @ps2goat points out.
  2. You could run into user experience problems. If a user bookmarks a page, and then later changes his filters, and uses the bookmark to return to the earlier page, his filters would be reverted, which is probably not what he would have expected.
  3. It wouldn't look very pretty. All your urls on your site would look like /controller/action/?YearSelected=2014&QuarterSelected=1&Parameter3=2&Parameter4=8, which could also create SEO issues you would need to take care of.

In that case, I would recommend you to consider using a cookie or saving the user's filters on the server instead. (But preferably not in a Session, as that can create scalability problems for your application). If you used a cookie, the user's filters would be available to your Controller Action on each request automatically, as the cookie would be sent along with every request. (This is of course also something to have in mind when considering which strategy to use. If you have alot of cookie data, this will slow down the perceived responsiveness of your application, as the cookie data has to be sent along with every request to your server. So keep your cookie data as small as possible)

On the other hand, if you expect the user to change the filters often and maybe even several times on the same page, you could consider using jQuery to do an asynchronous POST to your MVC controller, retrieve the neccessary data using JSON, and update the view. This is actually not as difficult as it might sound.

What you would need to do to implement it, is to create a Javascript function on your page that submits your parameters to your controller action. You can send the data as JSON to the controller action also. Something like this could work: (Untested code)

<script>
    function submitFilters() {
        var parameters = {
            parameter1: $('#parameter1').val(),
            parameter2: $('#parameter2').val(),
            ...
        };
        $.ajax('@Url.Action("SomeController", "SomeAction")', {
            contentType: 'application/json',
            data: JSON.stringify(parameters),
            success: function(data) {
                alert('Received data back from server. Ready to update the view.');
            }
        };
    }
</script>

Then you would hook up the filters (Radio buttons, drop downs etc) to call the method submitFilters.

MVC automatically converts the JSON data it receives from the client into your C# ViewModel as long as the property names match. Then you can do whatever querying and filtering you need to on the server, and then send the data back as the result of the action. (Pseudo code)

public ActionResult SomeAction(MyViewModel vm)
{
    var data = SomeRepository.GetData(vm.Parameter1, vm.Parameter2, ...);
    return Json(data);
}

Then you just need to update the view when the data is received. You would preferably use a template engine for that, but it's not required of course.

This is just a crude example of how you could do it. Normally I would create a Knockout View Model to encapsulate it all. But this should get you started.

René
  • 9,880
  • 4
  • 43
  • 49
  • Thank you for reply, First thing is user change filters often(frequently). those parameters include year, quarter, view like filters so user change those values and he need to see update on the view as soon as possible. He navigate through different views but need have these parameters for other views also. I managed about 12 parameters as far now.no problem yet, as well as this is intranet application only limited to within organization. Views are large tables with consist of quarter wise values about 25 rows. and this is Intranet application no need to maintain SEO things. – unique Feb 07 '14 at 14:38
  • Okay, well in that case I would suggest you to consider a combined approach where you would store the filters in a cookie and update the view asynchronously as soon as the filters change. Then you won't run into any problems with a lengthy URL, or user experience problems with their bookmarks, and the view will refresh immediately when they change their filters. Their filters will also be remembered between visits, which is a user experience bonus. – René Feb 07 '14 at 15:47