Let's say that I have following situation.
I have many customers and many consultans, and each customer has attached only one consultant. So it's one-to-many relationship.
I created Edit view which display customer_name and customer_consultant_id how should I update information after post? Just UpdateModel does not work, so something more is needed. Any ideas?
To get the data from your web page to the controller, have a look at this article from Scott Hanselman:
ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries
Those following two links seems to be completly answering my question:
ASP.NET MVC, Entity Framework, One-to-Many and Many-to-Many INSERTS
ASP.NET MVC, Entity Framework, Modifying One-to-Many and Many-to-Many Relationships
Related
I'm Developing ASP.NET MVC Web Application project, and I'm using Entity Framework Database First Approach, So I would like to make validations on generated model classes, I know if i make validations on them directly, then my model validations will be overwritten every time my domain models are regenerated.
So I made a research, and found two approaches to use for this scenario:
1- Using buddy classes (How to add validation to my POCO(template) classes).
2- Using ViewModels and Auto-mapping them to my Entities (Designing an MVC repository using ViewModels
I see some sort of redundant code in these two methods, so, my question is:
Which one of the two approaches is best to flow?
1) This is the correct solution for adding validation metadata for the Entity Framework objects. The validation will be triggered automatically by EF before calling SaveChanges()
2) This is an aproach for creating Data Transfer Objects from your EF objects. You normally do this when you want to return the objects to the client (like in JSON format) - and you don't want to expose all the EF specific properties (like navigation properties, primary keys etc)
I am using Entity Framework Model First. There are 2 entities in my Model..
User
Role
I want to use MVC scaffolding (using VS 2012 or 2010) for this relationship.
From some posts in 20111 many-many was not supported. is it still not supported? If nto supported is there any workaround?
I can not change the Model (because database will be generated from it) however i am mainly looking forward creating Users, Creating Roles and Assigning roles to Users. if some workaround using one-many can give me above scaffolded views and controller I will be ok with that.
Thanks
Nachi
I am new to EF CodeFirst and trying to build relations between entities.
I've three tables and their relationship are as follows:
When I pass UserID I should get all the provider details of Organizations for which this user belongs to.
How to build the entities and their relations for this scenario in CodeFirst(fluent API) approach? I am using EF CodeFirst on existing database. So these three tables already exists in Database.
Any inputs or pointers will be appreciated. Thank you!
Use EF Power Tools. It has Reverse Engineer option that will create a model for an exisiting database. If you want to configure your model with fluent API instead of attributes you can use the above to get you to a starting point where you would manually convert whatever needs to be converted.
I am a new to asp.net mvc c#, Now I created an ADO.NET Entity Model , and retrieved all the table in my database to put in it. Now I have create a View in my project, and I want to take the data from the ADO.net entity to show in my view as the table or something else.
Could anyone show me, how can I query data from ADO.net entity in my model of asp.net mvc?
Thanks in advanced.
if you are new to mvc then i think that the great starting point would be to download projects http://nerddinner.codeplex.com/ and http://mvcmusicstore.codeplex.com/ and go from there. you will definitely get your answer from there.also see this to get the idea
http://www.asp.net/mvc/tutorials/older-versions/movie-database/create-a-movie-database-application-in-15-minutes-with-asp-net-mvc-cs
I'm new to MVC and even though there is a lot (and I do mean a lot) of information out there that is very useful - it's proofing very difficult to get a clear understanding on how to achieve my exact requirements with MVC 2.0.
I would like to set up a solution as follows:
Provide a web UI using an MVC 2.0 project.
Use Linq to SQL classes project for data persistence.
I have a two separate code modules that will need to access the above Linq to SQL model - so I won't be able to include my Linq to SQL model directly in the MVC project itself.
Also I have a Business Logic layer in front of my Linq to SQL project.
My questions are:
How do I set up the Model part of my MVC application to point to my Linq to SQL project via my BLL?
How do I perform web app validation? Can I use MVC 2.0 Model Validation? If not what are the alternatives?
Finally (and slightly aside) - What is the ViewModel and how does this differ from the Model?
So many questions. But this is an exciting new technology and data access issues aside, everything else I've got to grips with very quickly and I think MVC 2.0 is fantastic.
Thanks for any pointers you can provide.
How do I set up the Model part of my
MVC application to point to my Linq to
SQL project via my BLL?
Typically you'd use a repository pattern for this. Your controller has a reference to your repository - the repository returns your domain objects from your database. The MVC app has no knowledge LINQ to SQL exists.
How do I perform web app validation?
Can I use MVC 2.0 Model Validation? If
not what are the alternatives?
Put view models in your MVC project. These view models may closely align with your domain models but their concern is to be the presentation model. Put your data annotations for validation on these view models - the MVC framework will automatically ensure validation occurs on these view models decorated with data annotations. It's pluggable so you could use alternatives - but with MVC 2, it's baked in fairly well and this includes client side validation.
Finally (and slightly aside) - What is
the ViewModel and how does this differ
from the Model?
I partially answered this one above. the shape of your domain models may not be the shape you need to display your views - view models are great to bridge this gap. Additionally, even if the shape does match exactly - view models are still a good idea so that you can put UI validation code there and other presentation meta-data on them (since you do not want anything related to presentation logic on your domain model).
Here's link for view model patterns.
Hope this helps.
You can add a reference to the objects exposed from your BLL assembly and use them as your Models.
When you want to add validation to classes that are generated use buddy classes.
A ViewModel is a custom-shaped aggregate of Model data. There is exactly one per View, as the ViewModel's purpose is to surface exactly the data needed by a particular View in a convenient and concise way.
An example might be a View that contains both Order and OrderDetail information. A ViewModel can hold internal references to the repositories and business objects for each type. Properties of the ViewModel merge together the data from these objects.
ViewModels will be useful in your case also because you want your Models to be in a separate assembly. You can apply the DataAnnotations to ViewModel properties for validation. You would make the "raw" business object models internal properties of your ViewModels, and expose public methods to retrieve and persist data.