Show a multiple choice form in a "Create" : Asp.NET MVC - asp.net-mvc

well i'm working with ASP MVC crud operations with generated forms with Entity Framework, but not all fields are the ones that i need to be shown. I mean, for example, a Create operation for the model Town (name, country).
first, country field is a reference on the table Country (i must maybe display a combobox filled with all countries on the database)
second, a many-to-many relationship with Transport (car, bus, train) and Town : here a town can have many transports and a transport can be found in many touns. here is may question : Can i display here a multiple choice (a checkbox list) with all transports that exists in the database?
i really need help please, maybe some questions are a little bit similar to my request but i made a search but i found nothing that may help me.
Thank You All ^^

you have to bring county and transport data into your view. what i guess from your answer is that you are sending your entity directly to your view. you should rather create a viewmodel including enumeration of countries and transports and then on your view you can use
<%:Html.DropDownListFor(x=>x.CountryID, new SelectList(Model.Countries, "CountryID", "countryName"))%>
plz look at this questions to find more about selectlist binding with viewmodels

Related

Concept on displaying cross-table(conjunction) data in ASP MVC

Just a rookie in .NET MVC world and still learning
I created three EF models in framework, one is clients, one is order, and items, below is the relation:
Client Order Items
PK:ID PK Order.id PK Items.ID
... FK:Client.id ...
FK:Item.id
In this case I wanna display all the client information and the item details they've bought in one table, obviously I cannot use any DBcontext here. So what should I do to combine the three table's info and output that? Create a new model on those three?
Any ideas or article are very welcomed!
I would create a ViewModel with all of the Data that you want to display. This is the model that will get populated in the controller and then it would get passed to the View.
So in the View it would use the ViewModel and wouldn't need to know about the underlying Database Model.
And in the Controller you would get the data needed and populate the ViewModel and pass that model onto the View.
Here is a page with an examples. There are plenty more out there too. http://sampathloku.blogspot.com/2012/10/how-to-use-viewmodel-with-aspnet-mvc.html

One Controller with many views for table per type

I have a table per type structure, I want to write an mvc razor application where my only parameter in the mvc routing is the primary key ID. From there the application will establish which type in my tpt structure the ID belongs to then displays the view formatted for particular type.
In addition, based on the type of the ID the view will also contain information related to that ID in from other tables in the tpt structure.
E.g.
Bob is a person with ID 1234
Entity 1234 has comments in the comments table
Entity 1234 has an entry in the entity relationship table to Simon
I've read a number of articles about making a base class controller, but would my URL change from www.something.burp/1234?
Is it possible to have a set of Index, Edit, Create, Details views that have byType separation?
Just looking for some pointers or ideas for me to research.
Thanks for you help.
MVC4, EF4, C#, SQL 2008
Decided to do multi controller, it's not going to matter to the user if it goes between controllers.

How to model many-to-many relationships?

I have a problem with a model in a asp.net mvc3 application.
First of all, i have a table with items, each item has e.g.: id,name and description
where i also have a user with: id, name
It looks somewhat like:
class item {int id, string name, string description}
class user {int id, string name}
Now i also have a table which maps these two things to each other, with foreign keys.
so each user can have * items and each item also can have * users.
Now i need to create controller and view for the item. this works fine, the problem is, that i just create a item, but i want to create a item and the mapping to a user in one step.
how to solve this problem in mvc? (e.g. i just can add one model in a view and this is the item in the create process)
Short answer: you need to add drop-down of users while creating an item. This will help to link each item to a user.
However, your particulate issue seems to be in building many-to-many relationship in the model. How to do this? Well, this is already nicely explained here - ASP.NET MVC, Entity Framework, One-to-Many and Many-to-Many INSERTS
In addition, I would strongly suggest to go through - NerdDinner Tutorial. It will give you good start-up and solid background for development in asp.net mvc framework.
You may also refer to Tutorial samples in official website to learn and practice development in mvc.
Here is a link to complete code of the tutorial - NerdDinner 2.0 Complete ASP.NET MVC Sample App

Editing only a partial model in ASP.Net MVC

I'm quite new to MVC, and stumbled on a problem. I've googled a lot but couldn't find a solution.
I'm using ASP.Net Membership with roles.
Lets say I have a model of a product with attributes:
Name
Art no
Category
How can I implement this so different roles cab only be allowed to edit parts of the object?
(Let's say one role cannot change the category of a product, for example.)
Is it possible to have different Views for the same Model or different Models for the same object?
If I leave out some of the properties, they will have NULL value when I save them.
I tried using #HTML.HiddenFor(...) but then the validation for those fields failed.
A ViewModel sounds like it would do the trick. For all but the most trivial of scenarios, you will get into problems when you tightly couple the Model and the View.
If you havent used them before, a ViewModel is simply a class (model) for the specific view you are rendering. You can customize required properties and validation on the ViewModel and then bind it to the Model, so the structure is most more flexible and easy to work with.
There is a detailed intro at ViewModels http://kazimanzurrashid.com/posts/asp-dot-net-mvc-viewmodel-usage-and-pick-your-best-pattern
EDIT
You could then have a ViewModel for each of the role, although if you are only looking to protect a property from being updated by certain roles there should be other solutions such as setting the html input to disabled and then testing on the server that the category value is still in its original state (note you should always perform such a test as the Post request can be altered).

ASP.NET MVC ViewModel to Hold Lists & "Images or YouTube URL's"

I'm posting this question because I do not know the best/correct way of doing the following.
My team-mate (the designer) sent me a good looking design that includes a wizard for adding new items (for auction). The user has to fill in all the required details which include the title, description, starting price...etc AND a list of tags (up to 4 tags - chosen from the database, will use auto complete) as well as a list of up to 3 images/youtube url's (for the sake of better explanation check this image out: http://i55.tinypic.com/2v11zzr.png)
Ok so I figured out how I should do the wizard ( reference: how to make a wizard with ASP.Net MVC) but I'm not sure about how to collect the lists and the images/url's. Here's what I'm thinking:
For the images/url's, I should create a parent view model from which two sub-classes (ImageViewModel & YoutubeUrlViewModel) would inherit from and then in the controller action when I parse the post data, I would check to see the instance of the parent view model and act accordingly.
Now about the lists, I'm not sure whether I should include a List in my view model or whether I should include 4 string properties representing the tags (the same will apply to the list of images/url's).
So what's the best way of doing this?
And Haacked to the rescue: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
:)

Resources