简体   繁体   中英

Handling Parameters of ASP.NET MVC Application

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM