MVC Filtering, how do you sort artists for a music site by A-Z? - asp.net

I have made the MVC Music Store through the tutorials and I'm now currently changing and adding things to the site to my liking. What I'd like to be able to do is open the Artist page and have the letters A-Z and All written across the screen in a bar at the top to allow users to browse Artists by the first letter of the bands name. What method would I use for this? Would a parameter help me?

You can use a parameter to filter data. When you populated model in controller action use default filter parameter as either 'All' or 'A' (first letter). Then filter model data and then return it with view. For 'All' obviously skip filtering logic.
On view you can have ActionLink for every letter linked to same action and passing letter as parameter. This way you can use same action to display data and all you will need action links passing parameter to that action and displaying data will be same (either use webgrid or any other way to display data).

Related

How to use Model values in view page with out model attributes?

I want to use model values on view page but in shortcode way?
For Example:
#model.name prints name which is assigned to model by controller.
But I want to use it with out #model.name by another token or shortcode?
For example now we use #model.Name for print its value on view page but I want that value with out using #model.Name.
I want to use another token which can print the same value of it.
I have found a solution; I can use tuple with view bag.
A view bag is a dynamic object using session, so make a tuple and then pass it to view.

asp.net mvc 2, select content by routing string

I am having trouble to what i should name this in the title field...
Question is. I want to select a blogpost by entering its title, like this:
http://localhost:3254/blog/2010/10/net-programming
The title of the blogpost should be ".net programming". So i have setup a route to look for a year a month and finally a string. But how should i save this in the database to be able to search for it?
Should i add a field, like id, that contains asp-programming? or should i parse the title for every search? or should i convert the title and then convert it back before displaying it to the user?
The standard method is to create a separate column in the database (usually called 'slug' or permalink) that stores the html-friendly name that may be used by Urls. Then, if the title of the post changes you would normally keep the slug the same so that anyone who has bookmarked the link can still find the post. The value us usually defaulted from the blog title, replacing spaces with - and removing any non url friendly characters.
http://en.wikipedia.org/wiki/Slug_%28web_publishing%29
There are two possible solutions I can think of
Create your own Route handler and within its code, search for the actual record in the database
Create an action method in a Controller that accepts a string parameter and call that parameter id (or whatever the default is in your MapRoutes method).

Views 1: Filter by custom table/field (or using Argument Hand. Code)

I have a page which should list nodes. The views is called from a locality page (a taxonomy term page). What I need is almost the same as using the Taxonomy: tid in arguments and passing the tid.
I can't use the term_node table, as (for other reasons) we have a custom table term_node_hierarchy (with nid and tid only). The table term_node_hierarchy is like term_node but also saves the tid of the parents (from an "external" code)
I've been looking for options but still no joy.
Currently I'm building an array of the nid's that should be displayed on the current page, and passing them like print views_build_view('embed', $view, $matching_nids); but the Argument Node: ID states This argument is a single Node ID. As said, only the first node is displayed when printing the views. It would be great if it could filter on more than one nid.
I'm open to any kind of suggestions on how to do this.
Thanks
You could create your own module for this. You could populate the $page_content variable with the results of your own custom query where you allow the user to sort against multiple nids. You could do this a number of different ways. You could display a list of the existing nids with corresponding checkboxes, so that, when the user clicks submit, all the nids that match the selected checkboxes get used in the query. Then you just display the result of that query. That's the easiest way I can think of to offer that degree of flexibility.

Processing variable number of form fields

I am working on a form which displays information about orders. Each order has a unique id, but they are not necessarily sequential on the form. Also, the number of fields can vary (one field per row on the form). The input into the form will not be mapped straight into the database, but will be added to the current value in the database, and then saved. An example of the form is in the picture below - the callout on the right shows the id for each row.
I know how to generate the form like this, but I can't work out how I can easily process each of these rows reliably. I also know how to give each of the fields a unique identifier, like name="order-23" or name="order[23]", but how can I translate that name so that I can update the related record in the database?
EDIT: One solution I can think of would be to iterate through every form field in the FormCollection, and if the name of the field matches the pattern, then I will extract the number from that field-name and process it.
However, I feel that there must be a much easier way to go about it - this method would likely involve a fair bit of string processing on each field, and there would possibly fall over if I have to add extra fields for each row later on.
Don't you have a list of IDs after postback? I believe you should not depend on what IDs are actually sent from the form, as anybody could change the IDs on the form to whatever they want, so it's a security issue. So you should after postback have a list of IDs you want to update (the same list you used to create the form with). In that case, you know exactly what id string you should use to retrieve the value from FormCollection.
In case you really can't get the list of IDs you are going to update, just use the FormCollection iteration as you suggested in your comment. The string processing is not that expensive in comparation with all other stuff being done at request processing.
If you have the names, then simply read the values by using Request.Form["order-23"] or re-create the controls in page pre-init and you'll have access to the values in your save event directly through the created controls.
I've done this loads in my CMS.
Essentially i sort of cheated.
The idea is something like this ....
render the form to the client, then look at the source code gneerated.
You should see that it generated a form tag with an action attribute.
When you click the submit button the form will be sent to that url so in the case of an order submission you would post the page back to OrderPage.aspx?OrderId=xxxx
then on the server you would build an update statement for your db that contained something like ...
"Update orders where order id =" + request.querystring["OrderId"]
So how do you update the action ...
In the calling page lets say you have a link called "New Order", when that link is clicked you need to do 2 things ...
redirect to this page.
generate an order id for this new order.
ok the first is simple, simply link it to this page.
the second ...
if this page is not a postback if(!IsPostback) { /* get a new id */ } depending on your order id's this may involve generating a new guid or doing something like getting the next number in a list by doing a select max(id) from a db.
once you have this new id you should still be in the page_load event.
this.form.Action = this.form.Action + "?OrderId=" + yourNewOrderId;
... tada ...
Send page back to the client.
view the source.

Combine two views into one view

I have two views that I would like to combine into one.
The first view shows all items of X where company ID = Y. This is to give preferential sort to the client first, and then everyone else.
So I created a second view, all items of X, where company ID != Y.
I created it as an Attachment to attach to the first view, but I don't think I got the intended result.
How can I combine these views so the first view results are listed first, and then the second view is too, using the same pager, filters, and arguments?
Is there any way of achieving this without programming it?
From a MySQL point of view, the order-by-field syntax would be the appropriate way to handle this. Example:
SELECT * FROM tickets ORDER BY FIELD(priority, 'High', 'Normal', 'Low');
It would be great if there is a module that add this kind of functionality to Views, but AFAIK, it does not exist.
If you want to solve this without programming, I think you can use the rules module to automatically set the 'sticky' checkbox on nodes where company ID = Y. With that in place, you can order the View on the sticky value.
Along the lines of the 'sticky' idea, if you didn't want to override that, maybe you could add a checkbox field to the company type -- isClient. Make it false for everyone except the client, and sort by that.
I haven't done this, but maybe you need to create both versions as attachments, and attach them both to another display...?
for drupal 5 there was views union. Someone started something for D6, but I don't know how far they got.
http://drupal.org/node/275162
Create the second view as an attachment and attach it to first.
Set all Inherit arguments, Inherit exposed filters and Inherit pager to Yes.
how is the client parameter passed to the view? as an url argument? if so, you can create your second view like i outline here and then select the exclude the argument option on the appropriate location.
usually the easiest way to achieve this is with a small hook_query_alter, but that requires a small amount of programming.
A little bit later... but I've found a better solution using only the Views module:
Create a Block View with that shows the first list that you need ("all items of X where company ID = Y")
Create another View that must be a "Page view" with the second list (all items of X, where company ID != Y)
In the "HEADER" settings of this second view, click "Add" and select "Global: View area".
In the "View to insert" list, select the first you have created (and check "Inherid contextual filters" if you are using it)
And that's it!

Resources