Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net MVC 4, How making Master Detail in the same form

I use entity Framework 4.2 and MVC 4

I Got this model/Database structure

UserInformation
UserID(PK)
FirstName
LastName
Email


UserFavoriteColor
FavID(PK)
Color
Why
UserID(FK)

Is it possible in one Create Controller Action to fill the UserInformation table and then Fill the UserFavoriteColor.

I Know I could perform this in two steps by creating 2 separates sectiosn. But this is not what I want.


enter image description here

like image 484
Jean-Francois Avatar asked Feb 05 '12 21:02

Jean-Francois


People also ask

Which component of ASP.NET MVC application does the same task of as that of master page in ASP.NET application?

Layout is similar to the master page in ASP.NET Web Form. Similar to master page, the Layouts may contain CSS, jQuery files and multiple views. Layout can keep the user interface elements and theme consistency throughout the application.

Can you mix webforms and MVC?

Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy. The reason for this is that the ASP.NET MVC framework has been built on top of ASP.NET.

How to implement advance master details entry form in MVC?

Just follow the following steps in order to implement "Advance master details entry form in asp.net MVC". Step - 1: Create New Project. Go to File > New > Project > ASP.NET Web Application (under web) > Entry Application Name > Click OK > Select Empty template > Checked MVC (under "Add folders and core references for" option) > OK

How to create MVC forms in ASP NET?

There are four various methods to create the ASP.NET MVC Forms with easy way, they are as follows, In the user interface view, there will be textbox field and submit buttons, it contains three textbox fields created for getting the values for ID of the person, name, and city of the person by using the Html.TextBoxFor method.

How do I create a master detail?

To have a proper master detail you need to actually have 2 tables. So in our case it really should be for example, the customer with their order Details, or the customer and their products, or even the products and the orders for the products. Showing the same table, in different views, is simply showing the same view in different ways.

How to create a database in ASP NET MVC?

Step - 1: Create New Project. Go to File > New > Project > ASP.NET Web Application (under web) > Entry Application Name > Click OK > Select Empty template > Checked MVC (under "Add folders and core references for" option) > OK Step-2: Add a Database. Now I will create a database for our application.


1 Answers

Typically, you would use jQuery to insert a new row. Since we don't know what your code looks like, it's hard to show you exactly how this should be done, but you can look at the examples here:

http://ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1/

The trick is that you have to name them appropriately so that the model binder will add them to your collection when you click save. Then you have to write code in your post method to walk through the list of colors and add any records that don't exist already.

This is a relatively complex thing, so it's not something that can be easily covered in a single answer here.

Another option is to simply have an action for the add-new button, and this inserts a blank record into the data collection, which on postback will now get 3 records (one of them with null values). When you fill in the values, it will then postback to the main post method and udate the blank record.

This solution has the drawback that if the user adds a new record and doesn't save, the blank record stays in the database.

like image 72
Erik Funkenbusch Avatar answered Nov 15 '22 09:11

Erik Funkenbusch