Multiple DropDownLists and a View and View model - asp.net-mvc

A design question on multiple DropDownLists on a View.
What is the best approach to displaying multiple DropDownLists on a View? Is it possible to pass multiple DropDownLists to a View model?
For example:
If I had the following View models: Student, Course, and Enrollment. And on the Index View page, I'm returning and displaying all students. But I'd like to filter the number of Students being returned based on values selected from multiple DropDownLists, such as Suburb, LanguageSpoken...
Each DropDownList will be populated from the database.
Two approaches that I can think:
In the Student View model, have a (fully populated) collection of Suburb, LanguageSpoken... .
Have multiple Models passed to the View model. In this case one Model will be a Student, and the other Models will be each DropDownLists. I haven't looked into the details of this yet.
I don't like the first approach, as it sounds very inefficient, i.e. each Student hold collections of list of all Suburbs, Languages... Also, not sure if the second approach is possible or even a good idea.
I'm using ASP.NET MVC 5 and Entity Framework 6.

Ok, after alot of reading up and trying a number of things, I've finally have a (simple) solution: Drop Down Lists using ViewBag.
E.g. the Student controller returns a Student viewmodel to the View, and any further models such as postcodes, languages... are returned via ViewBag to the View.
I figure ViewBag is appropriately used for such data, i.e. lookup (static) type data.

Related

How to display columns from two tables in MVC?

Right. So I am new to MVC.
I have two tables. tblCustomer_Details and tblCustomer_Location. There is a relationship between the two tables by CustomerID.
Now, how do I display few columns from one table and few from others?
I have tried using View Model. I created a new model named ViewModel and added Lists of both tables.
How do I proceed from here?

Splitting rails models vs. using one model for a combined form

I am working on an application that allows various people from different departments in a company log in and edit a form. Each department has specific fields in the form that only they are allowed to edit and view.
Would it be more organized code if I create one model for the entire set of fields and based on who logs in, render a different form containing the necessary fields? Or would it be better to split the department-specific fields into their own models and then based on who logs in, render a form containing their specific fields and relate the records from different departments together through a foreign key of some sort?
"It depends" is probably the best answer.
How many fields do you have? How much logic exists per department form type?
If you split all the fields to separate models (and corresponding tables) it will become a hassle if you ever want a full overview of the forms after they have been filled in, because they would be in separate tables. You'd have to go through every association of the form to get the full form.
If you still want to split them, because you have a lot of logic per department, you could point all your models to one 'main' table that contains all the fields, using table_name. That way you have all the data in one table and you can easily retrieve it. However, you'd also have to save the type of form in order to retrieve it in the correct model, probably more trouble than it's worth. (If it's the best thing to save all your fields in one table depends as well, how often does it get retrieved or does it get inserted/updated more often.)
Simple forms will probably fit just fine in one model. Unless you have a lot of non-sharable code per department it probably isn't worth it to split the form into separate models. If you do split the form into models you probably should create one main form model and have the department models extend it for whatever bit of reusable form logic there is.
After all that, I would say, I prefer simplicity so I'd probably save it in one model.
I'd split the fields and then you can setup your models with accepts_nested_attributes_for to build your form appropriately.

Using ViewModel with LLBLGen

I am using MVC 3 and LLBLGen. I am confused about approach on populating ViewModels that have data coming from multiple tables. How would i display the Foreign Key names instead of just ID? I don't have any virtual keywords like entity framework. Should i just resort to using SQL Views and then populate viewmodels through them? Just to elaborate more, let's say i want to display more than 100 results at a time and each result has multiple associated foreign keys then how would i display their relevant names rather just IDs?
IMHO, your choices are:
Fields Mapped onto related fields. This has the advantage that you are still using normal Entities.
TypedLists. This is like views, but the building blocks are entity relations and entity fields.
Use TypedViews, that are actually DB Views.
Use your own types, like DTO's or JSON projections. After all you are doing a kind of mapping between your business facade objects and the objects you use to show in your UI.

EF Joins and MVC Razor Views

I have several tables whose relationships are based upon unique key constraints.
A quick example is:
VersionId, VersionName
SurveyId, SurveyName, VersionId
QuestionId, QuestionName, SurveyId, VersionId
EF doesn't currently support relationships based upon unique key constraints. In my index view for question what's the best way handle the join to survey to show the grid of questions with survey name with respect to a model?
Do I need an anonymous type? db.Questions.Include("Surveys") doesn't seem to do anything. I could use linq and make a ViewModel of the joined tables (I suspect this is the way to go), but there are so many things in EF & MVC that I thought I'd check before doing anything.
Why do you have a link to Version (i.e. VersionID) in both your survey table and your question table? Couldn't you reach the version from the question through the survey?
Also, if you have the relationship between Question and Survey is many-to-one or one-to-one (each question only has one survey) then it should be db.Questions.Include("Survey") - non-plural.
First of all, no joins (or practically any other logic) in razors views. Controller is the place to build up the ViewModel, and views are means to just present that ViewModel. As you mentioned, making ViewModel for it is only (correct) way to go. And you can fill that viewmodel by whatever method - linq is absolutely normal way to create joined data. And if you want to go further, you should place that join logic in some kind of repository, for example QuestionRepository, not in controller.

Rails model to represent multiple fields

I'm developing a rails project where I have one data model with multiple fields that are collection selects. I'd like to create another model to represent all of these collection select fields. So, for instance, my main data model has three collection select fields -- one for county, one for category, and one for classification. I could separate these into three separate data models, but that seems redundant since they all share the same characteristics. They have a type and a value, like a county is a county and it has a value of let's say Sonoma, just as category has a type of category and a value of let's say Winery. If you've ever used Drupal, I'm basically looking for the behavior of the taxonomy functionality.
So you see my dilemma: I need to separate these fields into three separate fields but they have very similar data structures. Any suggestions would be greatly appreciated.
This is a perfect case for single-table inheritance. Your problem is screaming for it.

Resources