ASP.NET MVC 3: Cascading ComboBox with EF - c#

Hi fellow StackOverflow Members,
I'm tired of bumping my head on the wall, so I would like to explain my problem here, hoping to get a solution for it.
I'm developing an ASP.NET applications, using C# and Razor. I've also implemented the Telerik.MVC.UI for Grid displays.
What I'm trying to do now is, on my Create form, show some sort of Cascading Comboboxes.
I found a lot of tutorials on the internet, explaining how, however, my database structure isn't
identical.
Most tutorials work with 2 or 3 tables (Entities), but all my data fields are in one table.
My table is constructed as follows:
ID Division Department Country
So I would like to have the first dropdown to show a SELECT DISTINCT of Country, next I would like to see the matching Departments, and last I'm searching for the matching Divisions.
As mentionned above, this is all within a create form, so upon POST, I want to pass the related ID to the Controller Action.
Any help is appreciated, as I'm totally stuck...
Thanks in advance.

Use strongly typed view models. Make a viewmodel for your division, department, and country (basically a class which holds a list<> of each field from your db table). In your controller fill the viewmodel holding all three from the database and then pass it in return view(viewmodel). In your view you can then use someHtmlHelperFor(model.division or model.department,etc.).

Related

Sitecore: Drop Down Search Menu

I am in need of hopefully an example for a drop down list search function. Not sure what the correct terminology would be.
Needed:
Two drop down lists then a submit button. First being of Country, then the second being of Department. So say you picked France and HR, it would display a contact person below.
Is there any examples of something of this sort? Would be nice have code in CS then have my end user be able to add and link the two together in Sitecore. So they could add Countries they would like, departments they would like, then the respected person linked to the two fields.
So if I understand you correctly, you're taking multiple inputs and producing results for that combination of them. I'll make some assumptions and try to give you enough direction to get started. This will take a bit of explaining.
Starting from scratch, we've got a Contact template that has, at least, a Country and Department field. You'll need templates and lists of items for each of those two fields. You could get these values from another source, but let's just keep it simple for now and use lists of items. This sounds hierarchical, so I'd suggest you define your departments as children of your Country items. What other fields are on these items doesn't really matter at the moment.
Next, you'll want to communicate the end-user's selections back to your Sitecore instance. I'll assume that you're using Sitecore MVC, but the same principles apply for web forms.
Set up a controller to parse your parameters, country and department, and a form in your View with <select> elements populated from the lists of countries and departments. When the country selector is changed, you can either reload the page with the value set as a request parameter or use AJAX to ask your controller directly. If you structure the model so that it doesn't rely on the rendering context, you can submit your form results to /api/sitecore/{controllername}/{actionname}?country={values}&department={values} and get the data that way. (If you want an example of the kind of JS you need to do this I can provide one.) You could disable or hide the Department <select> until this is selected so that users don't get confused, then use the same technique to also set the Department parameter/submit that value. Once those two values are in the request, you have enough information to get a list or single Contact and display that information, also using the same technique.
If you have a more specific question about a particular aspect of this process I'd be happy to explain.

ASP.Net MVC Dashboard View. Where to perform aggregations

I have read a few other stackoverflow posts regarding dashboard setup in a view, but I am still struggling a bit. I have one master query that is returning a detailed view of my data, since I only want to hit the database once. So I have a Controller Action called GetEmployeeInfo() that returns a IEnumerable<EmployeeBase> model class. I want to be able to take this class and break it apart into different divs in a view. For example, I would have three divs on top of the View aggregating row count by employee category and 2 divs on the botton that shows the detailed table. Can this be accomplished strictly through one view by setting up conditional/aggregate statements all over or do I use partial views or something else. Or is using one controller action a problem?
Thank you.
I used the answer from Pat Burke (see comments under my question. I posted it below as well). I created one base class and one partial view for my employee categories that has all the properties I needed. I did the same for my detail portion. Then I created the DashboardViewModel which had 3 IEnumerable<EmployeeCategoryBase> properties as well as 2 IEnumerable<EmployeeDetailInfo> properties. Then I created a strongly typed DashboardViewModel view with 5 divs that used #{Html.RenderPartial(ViewName,model.property);}
Another alternative is to have your Dashboard view strongly typed to a View Model like "DashboardViewModel" which is composed of different collections which correspond to the data needed for each sub-part. This would also put the aggregation logic to the server-side rather than on the client in JS. You could even have partial views included, each of which corresponds to a sub-part and a collection of data in your View Model. It's good practice to have your view model directly correlate to the data needed by your view

Editing a model plus a list of it's child elements on one page

Okay so I have a Product entity which has one to many prices, (depending on the quantity, a price is determined). I want to be able to edit these prices directly from my product controller, but I'm not really sure what the best practice to achieve this is.
I've tried using PartialViews but the databinding wouldn't work. Then I found http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/, which would be good to bulk edit the prices, but I don't know how to combine these with my parent (Product) model (using a viewmodel maybe)?
Could anyone link me to an up to date example of this? Since it's nothing really out of the ordinary what I'm trying to achieve.
Found the correct answer at the following URL: http://jarrettmeyer.com/post/2995732471/nested-collection-models-in-asp-net-mvc-3

How do I pass values from a database between pages in WPF?

I created an application that connects to an SDF database. The user is given a list of movies in a cinema. The user can select a movie and press an 'ok' button.
The user should then be presented with another datagrid which contains all the information of that movie.
I've tried passing the SelectedIndex between the two pages, it sends the SelectedIndex to the information page, but it won't assign it to the same SelectedIndex.
public Desc(int input_id)
{
InitializeComponent();
cinemaEntities = new cinema1.cinemaDBEntities();
movieViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("moviesViewSource")));
System.Data.Objects.ObjectQuery<cinema1.movie> moviesQuery = this.GetMovieQuery(cinemaEntities);
movieViewSource.Source = moviesQuery.Execute(System.Data.Objects.MergeOption.OverwriteChanges);
moviesListView.SelectedIndex = id;
What to do depends on the purpose of the software, but in any case I would recommend to spend a little more effort on the architecture of your software. As you want to use WPF, you should decide whether to go for a MVVM (Model-View-ViewModel) approach which is highly maintainable and has numerous advantages, but demands some time to get familiar with. The quick solution which is absoulutely fine for small or simple apllications is to code your GUI logic in the codebehind of your views and controls.
Anyway, I would create a model layer which mirrors your database data in according types (MovieDatabase has a Collection of Movies, etc. etc.). Then write an adapter to fill the model from the database.Then either use the model in your views - if you want to do it quickly - or write ViewModels to your Models (which is better) and use those in your views.
This being said, from the code you posted its hard to tell, what the problem is. Do you have a little bit more context? Why don't you pass the SelectedItem?
Why not just pass the Movie object to the second page? And then use .SelectedItem. Why does the second page need the whole list anyway if it is detail for just one movie?

Sending collection of items in ASP.NET MVC

I've got two classes in my MVC project Order and Product.
Because Product can be ordered many times and Order can have many products I've got third entity which is OrderedProduct. It joins those two entities in many-to-many relation.
Now what I'm trying to do is to let user to make an order by putting products from the drop down list to the box and then to save my order. Also client have to fill some fileds in the Order entity such as Address data etc. All I want is to have it all on one single page. User can add as many items from dropdown as he like, then he can add whole order.
To make it easier to visualize look at this picture:
Now the problem is how to implement such behaviour in my MVC app. Should I build a ViewModel that combines Order class and list of Product or use partial view for Product classes?
Also which is my main problem, how can I (in elegant way) retrieve the full list of chosen products or at least product id's in the controller after POST request? In this case how can I specify that what I'm sending is a collection of ids? It's simple to add one object, but what about whole collection?
This is the place when I do not fully understand asp.net MVC, so please give me some bright ideas ;) Greetings to you all, thanks in advice for all your answers!
The desired interface seems a bit confusing to me but here's how you can handle these things.. modify to your desire. I'm going to assume OrderedProduct is an object for ordering that contains a Product ID and a quantity and that you want to be able to modify them all at the same time.
Make Order your ViewModel. Given that it has a List<OrderedProduct> property called OrderedProducts:
Create a small editor control with a ViewModel of OrderedProduct. No form, just a textbox/dropdown/whatever bound to the product name property and a textbox bound to the product quantity. Important: put this control in views/shared/EditorTemplates and call it OrderedProduct.ascx.
Give your Order object a property called NewOrderedProduct of type OrderedProduct.
In the form for the Order view do: <%=Html.EditorFor(m=>m.OrderedProducts)%> this will give you an editable list current items in the order.
After that do: <%= Html.EditorFor(m=> m.NewOrderedProduct) %> this is where you can add new items.
Have the [POST] action take a type of Order.
Now when it submits if the NewOrderedProduct property is valid you can add it to the OrderedProducts list and redisplay. You can add as many as you want this way and they will all autobind.
Have a separate submit button in the form for submitting the order. Check for the existence of that button's name in the [POST] and submit all at one time.
This gets you a straight HTML working version. From there you can Ajaxify it if you want.
Edit
Updated to reflect the function of the OrderedProduct object and correct the type the post accepts

Categories