Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mvc actionlink with jquery 1

Status
Not open for further replies.

dinger2121

Programmer
Sep 11, 2007
439
US
Hello,
I am trying to use the jquery ui datepicker in an mvc application. I am able to display everything just fine. The problem I am running into is getting the value from the datepicker control to use in an Actionlink.

I would like to use the datepicker for filtering the data on the page by a date date range. I was hoping to use an Actionlink which the user will click on after choosing the dates. This actionlink will construct the querystring values from the values of the datepickers.I will then use the query string to pull out the dates to filter by and pass them back to the same page.

I have create an htmlhelper class which defines the datepicker. I was thinking I could define a property which will return the value of the datepicker, but I don't know how this would be set up.

I hope all this makes sense.

thanks for any thoughts.

carl
MCSD, MCTS:MOSS
 
it sounds like your mixing server (ActionLink) and client code (jquery). the actionlink can render the default url, but once a date is selected by the datepicker you will need to change the link at the client, so ActionLinks are out.

look into the jqueryui datepicker documentation. I'm sure there is a dateselected or datechanged event. hook into this event and change the url.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
that's what I thought - I didn't think I would be able to do that.
That being said, I have a datagrid in my mvc application which I would like to be able to filter and sort on various criteria. Generally there will not be more than 100 - 150 records returned. Would I be better off requerying the database each time a filter is updated, or would I be better off using jquery?
Correct me if I'm wrong, but if I want to be able to maintain urls to filtered data (so people can bookmark filtered data), I will have to a server side query for each filter, correct?

carl
MCSD, MCTS:MOSS
 
Generally there will not be more than 100 - 150 records returned. Would I be better off requerying the database each time a filter is updated, or would I be better off using jquery?
I don't see the connection between querying the database and jquery. jquery deals with UI concerns. the closest it comes to server functionality is an ajax request. but it's just that, a request. jquery has no idea how the request is processed. so whether you requery the database, pull from cache, or someother intermediate process, jquery doesn't matter.

Now, how would I handle the querying?
1. I would query the database for each request
2. I would page the results instead of returning all records
3. If I require bookmarks I would use perma-links (or whatever, they are called) like Google uses with maps. A click-this-link-to-bookmark sort of thing.

how you query the data and structure the database(s) is another matter and most likely outside the scope of this post. If you're curious checkout Udi's latest post and research OLAP vs OLTP. in some scenarios having a separate database for analysis can provide a huge preformance boost. The trade of is managing the complexity.
Correct me if I'm wrong, but if I want to be able to maintain urls to filtered data (so people can bookmark filtered data), I will have to a server side query for each filter, correct?
this depends on how you structure your controller(s) and query(s). I use Monorail for my MVC so the syntax may be slightly different, but the idea is the same.
the search controller would look something like this
Code:
class SearchController: SmartDispatchController
{
    public void FindRecords([Databind("filter")] SearchCriteria userInput, [Databind("paging")] PagingInfo page)
    {
        var results = //construct query with paging;
        PropertyBag["Results"] = results;
        RenderView("Location", "Name");
    }
}
with MS MVC I think it would look like this
Code:
class SearchController: SmartDispatchController
{
    public Action FindRecords(SearchCriteria userInput, PagingInfo page)
    {
        var results = //construct query with paging;
        View.Model = results;
        return ViewAction("name of view");
    }
}
I use NHibernate as my data mapping layer, so dynamically constructing querys is simple. depending on your data access method of choice will determine how difficult it is to construct queries.

the view would look something like this
Code:
<script>
$(function(){
   $("bookmark").click(function(){
        var url = document.location.split('?')[0];
        var querystring = //convert form data to query string
        document.location = url+'?'+querystring;
   });
});
</script>
<form action="~/Search/FindRecords" method="POST">
   <input ... />
   <input ... />
   <input ... />
   <input type="submit" value="Search" />
</form>
<div id="results">....</div>
<input type="button" id="bookmark" value="Bookmark Results" />
what's happening here is the click event of bookmark will get the base url of the current page. it will then transform the form input values into a query string. finally the user will be redirected back to this page.
the user could then bookmark the page because the filter will be in the url as a query string, not the form.

you're on the right path.


Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thanks for your help again Jason.

carl
MCSD, MCTS:MOSS
 
no problem, I like help requests along these lines.
1. it's conceptual, not syntax
2. the request dealt with 2 of my favorite web technologies: MVC and jquery :)
3. it was really a question about web development.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Since it really doesn't make too much sense to use a jquery datepicker to filter for dates since it is a client side control and I will be requerying the db for filtered data. Can you recommend what I should be using instead? the .net calendar, or some 3rd party?

Also, this may be off topic, but is there a particular reason you use Monorail over asp mvc?

thanks again for your help.

carl
MCSD, MCTS:MOSS
 
Since it really doesn't make too much sense to use a jquery datepicker to filter for dates since it is a client side control and I will be requerying the db for filtered data. Can you recommend what I should be using instead? the .net calendar, or some 3rd party?
you're mixing concepts again.
presenting data to the user and querying the data base are 2 distinct processes.
how you present the search options and how you query the database are independent of each other.

your search controller accepts the user input. but how the input was entered doesn't matter. your search form will present the user with search options. what the controller does with the input doesn't matter.

so how you display a calendar to the user doesn't matter.

you will find that 3rd party web controls only exist for webforms. think about it. designing a rich UI control for webforms is about as complex (if not more) than designing a UI control for desktop apps. webforms makes a simple concept like html complicated. I think this is because webforms is designed to make the web as much like a desktop as possible.

html itself is very simple. and libraries like jquery make it even easier. there isn't a market for 3rd party controls because it's just javascript, css and html.

Also, this may be off topic, but is there a particular reason you use Monorail over asp mvc?
The biggest reason is I was using Monorail before MS MVC was out of beta.

Back when I was still using WebForms I latched onto the MS AJAX library when it was first released. With every beta and RC distribution was breaking changes. I decided after that not to trust "beta" and "rc" releases. When MVC hit, it was beta, and then RC. and with each release came breaking changes. I was glad I didn't switch frameworks.

Monorail also has a richer feature set. ViewComponents and DyanmicActions are the 2 most notable.

Monorail (as well as other OSS projects I use) heavily favor convention over configuration. The idea of an opinionated framework drastically reduces configuration and embraces the concept of least surprise. That does mean I have to play by the frameworks rules, but they rules make sense. Like "don't run with scissors", and "look both ways before crossing the street" :)

The one thing MS MVC does out of box, which Monorail doesn't is use strongly typed data for views and view actions. You can integrate the DictionaryAdapter to get strongly typed models, but it's not there out of the box.

Also, if you have heard of, or used, the Spark view engine yet I highly recommend it. writing html is so much easier because the server tags are gone! no more tag soup.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top