How to model many-to-many relationships? - asp.net-mvc

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

Related

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).

Show a multiple choice form in a "Create" : 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

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
:)

How to reuse logic in a context sensitive way in ASP.NET MVC

Trying to figure out the best way to organize a ASP.NET MVC site. Take a very simple 1..N relationship: Company can have many Contacts, Contacts must have exactly one Company.
I have your typical routes:
Company/Index (list all companies)
Company/Details/{int} (details of Company {int})
Company/Create (create new company)
Contact/Index (list all contacts)
Contact/Create (create new contact, company is selected from drop down)
Now if I wanted to create a page that created a Contact in the context of a Company (from the Company detail page) so that the required company is filled in/not editable), what would be the best route of going about that, while not duplicating code where possible.
Not sure if I can leverage the Contact/Create logic/view from the Company controller (and be able to route back to the Company Details page when complete), or mess with the routes to do something like Company/Details/{int}/Contact/Create (not even sure if that makes sense or would work)?
There has got to be a better way then me adding my logic and view for adding a Contact into my Controller view and having it duplicated.
Cant find the link, but found some information about using named routes to help out :)

How do I use multiple data sets in a view in ASP.NET MVC?

I'm just learning ASP.NET MVC and my first project is to create a simple link directory (like DMOZ).
I can easily build a strongly typed view of a list of subcategories for a category.
I can easily build a strongly typed view of a list of all the sites in a particular category.
Now, here's what I'm having trouble wrapping my head around:
If I'm viewing a particular category, how would I, in the same page view, display two models (sets) of data:
Top of the page: All subcategories for the category being viewed.
Bottom of the page: All sites in the category being viewed.
I don't have the faintest idea of how to return both the subcategory list and the site list to a particular view. Is it possible? Is there a clean way to do it? (Feel free to point me to an online tutorial or book chapter).
There are two approaches: You can either store one list in ViewData and have it not be strongly typed in your view or you can create a separate ViewModel class which takes two or more existing models so you can refer to these models as properties of your strongly typed ViewModel class inside the View itself.
The best source of information I found was the sample chapter of the upcoming ASP.NET MVC 1.0 book. The first chapter was written by Scott Guthrie and can be found here: http://aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf

Resources