Adding And Removing Items Dynamically In One View With Entity Framework And MVC

Mar 1, 2011

I've been at this same question in different forms now for a while (see e.g. Entity Framework and MVC 3: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. ), and it's still bugging me, so I thought I'd put it a little more generically:

You have an entity object (using Entity Framework), say User. The User has some simple properties such as FirstName, LastName, etc. But it also has some object property lists, take the proverbial example Emails, to make this simple. Email is often designed as a list of objects so that you can add to that object properties like Address and Type (Home, Work, etc). I'm using this as an example to keep it generic, but it could be anything, the point is, you want the user to be able to add an arbitrary number of these items. You should also be able to delete items (old address, or whatever).

Now, in a normal web page you would expect to be able to add these items in the same View. But MVC as it seems designed only makes it easy to do this if you call up an entirely new View just to add the address. (In the template for an Index View you get the "Create New" link e.g.).

I've come across a couple of examples that do something close to what I mean here:

[URL]

and

[URL]

The problem is, although the sample projects on these sites work fine, with mock model objects, and simply lists (not an object with a child list), it's a different thing if you actually want to do something with the posted information - in my case save to database through the Entity Framework model. To adapt these cases to that, all of a sudden I'm in a maze of intricate and definitely not DRY code... Juggling objects with AutoMapper and whatnot, and the Entity Framework won't let you save and so on (see above link if you're interested in the details).

What I want to get at is, is it really possible that this is such an uncommon thing to want to do? Update a child collection in the same View as the parent object (such as the email addresses in this case)? It seems to me it can't be uncommon at all, and there must be a standard way of handling this sort of scenario, and I'm just missing it (and no one here so far has been able to point me to a straighforward solution, perhaps because I made it too abstract with my own application examples).

View 1 Replies


Similar Messages:

ADO.NET :: Adding A New Entity In Entity Framework 3.5 With Related Data?

Aug 5, 2010

I am trying to add a new entity and have to refernce associated data to add it. I cannot load the Referencetables. Giving "The EntityReference could not be loaded because it is not attached to an ObjectContext." How do i complete this task in Entity Framework 3.5

[Code]....

View 1 Replies

Removing All Items From A List View?

Jan 23, 2010

Another niggling issule. I am trying to remove all the databound items from my listview but it doesn't work. I am trying to remove the items as i have used the delete command button (alongside with the updateand edit)

I have tried.

listview1.items.clear();
listview1.datasource = null;
listview1.databind();

But none of these have worked. I have also tried removing the item via it's index.

View 5 Replies

Web Forms :: Removing / Adding Sidebar Links Dynamically

Feb 2, 2010

I am using Wizard control, my requirement is when drop down list selected index change event occurs , based on the selected item I should remove or disable some side bar links. I have seen many posts removing and adding dynamically. But I could able to remove dynamically but I could not able to add dynamically. I am getting Viewstate problem.

View 1 Replies

C# - Adding A Custom Property To Entity Framework?

Oct 19, 2010

I am using the Entity Framework for the first time and want to know if the following is possible - I have generated my classes from the DB, and have one called Category.

Obviously it has all my fields in the table (ID, CategoryName, SortOrder etc..) but I want to know if I can add a custom property which is not in the table, but is actually the result of a custom method.

I want to add a new property called 'CategoryURL' which is basically the 'CategoryName' property run through a custom method and returns a hyphenated string.

My initial thought is inheriting from the generated Category class and creating something like this inside?

public string CategoryURL
{
get{ return MyCustomMethod(this.CategoryName) }
}

Is this the correct approach? And will 'this.CategoryName' work as I think it should? Basically the end result is when I return a list of 'Category' I want this to be part of the class so I can use it in my foreach loop.

View 1 Replies

Adding To EntityCollection Adds To The End Of Collection In Entity Framework?

Feb 28, 2011

I'm trying to have a View where the user can add items in a collection without having to go to a new View (the scenario is a sort of CV site where the user adds info about work experience, skills, etc, and it would seem absurd to go to a new View to add each little thing).

So I have an edit View that shows a number of text boxes for the already added items, and there's an ajax call to go to a method to fetch the collection fresh if the user adds an item.

Here are the methods in question:

[code]....

View 1 Replies

C# - Dynamically Loading SQL Tables In Entity Framework?

Feb 18, 2011

I need to dynamically access some SQL tables hopefully using the Entity Framework. Here's some pseudo code:

var Account = DB.Accounts.SingleOrDefault(x => x.ID == 12345);

which will return me an Account object and this contains some fields called "PREFIX", "CAMPAIGN ID" and further information about the accounts are stored in separate SQL tables with the naming convention of PREFIX_CAMPAIGNID_MAIN.

The tables all have the same fields so I was thinking of creating a new Entity that isn't mapped anywhere and then dynamically loading it, like so:

var STA01_MAIN = new MyAccount(); // my "un-mapped" entity
DB.LoadTable('STA01_MAIN').LoadInto(STA01_MAIN);

I can now get anything about the STA01_MAIN account: STA01_MAIN.AccountId. So my question is: how do I access these tables using the Entity Framework?

View 1 Replies

Adding Data Into Tables Includes Foreign Keys But Which New Row Created Entity Framework

Apr 6, 2010

Adding data into kartlar table (RehberID,KampanyaID,BrimID) is ok. But which Kart'ID created? I need to learn which Id created after adding data (RehberID,KampanyaID,BrimID) into Kartlar?

[code]...

How can I do that? I want to get data from Kartlar which data I added?

View 1 Replies

Exporting Entity Framework 4 Data Model To Entity Framework 3.5?

Oct 25, 2010

Is there a way to export a EF 4.0 Data Model to EF 3.5?

I looked around and found that we are not able to access EF 4.0 from a ASP.Net 3.5 project here: [URL]

Our project is the 1st to go to .Net 4.0 using Entity Framework and we (the team) were wondering if there was a way for the other projects that "might" need to access our data that are still using the .Net 3.5 framework.

View 1 Replies

Dynamically Adding Items To An Unorder List In C#

Feb 22, 2010

if i create a html ul. and wanted to dynamically add list items to this list.

so i have :

<ul id="test" runat="server">
</ul>

is there a way i can add list item to this list dynamically in asp.net i am using vb

View 1 Replies

Forms Data Controls :: ListView With Datasource And Later Adding Items Dynamically?

Dec 11, 2010

I have a listview which has a complicated Item template that contains a repeater, an objectdatasource and some other controls. On the first run I am only showing 10 Items of the listview. The user has an option to show 10 more ListView Items. If user clicks on show older items; the listview shall add another 10 items. how to go around this since my list view already has a datasource and I don't want to rebind the whole listview all over again. Instead; I just want to add another 10 items.

View 7 Replies

Adding A 3rd Party Test Framework For MVC3 And Razor View Engine?

Feb 19, 2011

Would like to add a third party test framework (such as NUnit or MbUnit) to the drop-down list of test projects for the ASP.NET MVC3 type project in Visual Studio 2010.

This had worked before for ASP.NET MVC2 and prior, but the msdn instructions are not accurate and does not work. Creating the registry keys in the HKEY_CURRENT_USER seem to delete the keys every time the VS-2010 starts, the HKEY_LOCAL_MACHINE does not follow the same tree structure.

how to add a third party framework under ASP.NET MVC3?

View 3 Replies

ADO.NET :: Entity Framework With Stored Procedure Versus Entity Framework Without Stored Procedures

Nov 2, 2010

I am new to entity framework , it is really very good , but I want to know what is the difference between using entity framework with stored procedure or without stored procedure , which one the faster and what is the benefits for using stored procedures with entity framework.

View 1 Replies

Entity Framework -Update Entity When Another Entity Is Updated

Jun 30, 2010

I have an EDM, it includes the entities extension and history. My goal is to use history to keep track of all the changes made to extension entity. For example, if extension with ID 223 has its property 'Name_Display' changed - I want the history entity to record this.

I'm using ASP.NET with VB.NET. Where in my code do I put the hook to say, "update the history entity" and what should that hook look like?

View 1 Replies

C# - Dynamically Adding Columns To A Table In An MVC View

Apr 20, 2010

I am returning an IList to my model of data about users. I want my end users to be able to select which columns they want to see.

I know I can do this with a huge "if statement" however, I know there must be a better way. I have created an Enum with the column names. I feel if I could take the IListItem.EnumName I would be set. However, I'm not too sure on how to do this.

View 2 Replies

Cast Exception When Try To Insert Entity In Entity Framework (using Code-f)

Feb 9, 2011

I get an cast exception when i am trying to insert an entity in Entity Framework (using code-first). From this code :

public virtual T Insert(T entity)
{
return Context.Set<T>().Add(entity);
}

The cast exception is like "impossible to cast ...Collection'1(Entity) to type (Entity)" I can't figure out why. I am pretty sure ive done everything right. Post entity

public class Post
{
public long PostId { get; private set; }
public DateTime date { get; set; }
[Required]
public string Subject { get; set; }
public User User { get; set; }
public Category Category { get; set; }
[Required]
public string Body { get; set; }
public virtual ICollection<Tag> Tags { get; private set; }
public Post()
{
Category = new Category();
if (Tags == null)
Tags = new Collection<Tag>();
}................................

View 9 Replies

ADO.NET :: Entity Framework - Entity Stored Procedure Mapping - Can't Update

Feb 10, 2011

I am mapping a stored procedure to an entity by right clicking on the entity (in the .edmx) and selecting "Stored Procedure Mapping." This brings you to a Mapping Details - "Name of Entity" Window that allows you to select the insert, update, and delete stored procedures associated with the Entity. It also maps the stored procedure parameter to the Entity "Property" (Column).

I'm gettin an error "error 2042: Parameter Mapping specified is not valid." The cause of the error is fairly obvious, in the Insert stored procedure that has been selected, a 'CHAR' parameter is being mapped to an Int32 Entity Property. I altered the stored procedure parameter to match the entity, I deleted the stored procedure, readded, and reslected it as the Insert function. I also cleaned, validated, updated model from database. No matter what I do, the parameter list in the mapping details doesn't reflect the change to the stored procedure. It's stuck on a char --> int32 mapping, even though it has been changed, like it's buried deep in meta data some where.

View 2 Replies

MVC :: Adding / Deleting / Editing View Pages Dynamically / Programmatically>

Jul 27, 2010

What I want is that I can add /edit or delete view pages. I don't want a complex content management system , just looking for any examples where one can add simple view page, I have no idea how to add action result dynamically, programatically on a controller page in mvc2.

View 5 Replies

ADO.NET :: Entity Framework Copy Entity And Change PK And Save Old And New

Mar 31, 2011

I have a stupid problem with the Entity Framework. I get the following Exception:

[Code]....

I have an entity with a 4 fields representing the primary key. I copy it via serialization (works fine). I set the old entity to not valid (Datefield set to a date in the past, this field is part of the PK) and set the copied entity to DateTime.Now.Date. When I call context.AddObject I get the Exception above. I tried copying the entity via reflection but the entity has 3-4 references. And when copied, I get another Exception before even Adding the entity to the context. I also tried setting newObj.EntityKey = null and reset all the fields neccessary for the PK. I just want a whole copy of an entity with a different primary key (and some other fields changed too) and Create it in the database.

View 1 Replies

VS 2013 ASPX Menu - Dynamically Adding Items Display Outside Menu Control

Apr 24, 2015

I have placed an aspx menu control on my master page with orientation as Horizontal. Below is HTML markup.

HTML Code:
<asp:Menu ID="mnuMedChem" runat="server" Orientation="Horizontal" Width="100%" OnMenuItemClick="mnuMedChem_MenuItemClick">
<DynamicHoverStyle CssClass="DynamicHover" />
<DynamicMenuItemStyle CssClass="DynamicMenuItem" />

[Code]....

I have tried populating the menus from my database by using the

Code:
mnuMedChem.Items.Add
and
Code:
mnuMedChem.FindControl(value).ChildItems.Add

The menu items and the childs are populating correctly but then the menu is not being displayed on the menu control.

View 4 Replies

How To Simply Update Entity In Entity Framework

Sep 21, 2010

I'm writing a custom .NET MembershipProvider (not the built in one) and trying to update using Entity Framework. But of course i have no access to (Try)UpdateModel. How can i update it?

View 3 Replies

How To Remove / Delete 0..1 Entity In Entity Framework 4

Jan 5, 2011

I have an Events table and an InstallmentPlans table. The relationship is 0..1 : an Event can have 0 or 1 Installment plans. If I want to remove the existing InstallmentPlan for an event, how do I do this? Setting it to null doesn't seem to work:

_event.InstallmentPlan = null;

View 2 Replies

ADO.NET :: Entity Framework: Getting Key Of Newly Inserted Entity?

Feb 3, 2011

Here's a simple code snippet where I create a new Entity object and call SaveChanges() to create a new record in the mapped database table.

[Code]....

What I'm trying to figure out is whether there is a way to derive this new key for my entity, e, from the ObjectContext, m? The reason for this is that the way my actual code is structured, my entity object is already out of scope at the point where I call the SaveChanges() method.

View 4 Replies

ADO.NET :: Order By An Include Entity In Entity Framework?

Sep 24, 2010

I am trying to do something like this:

[Code]....


But ofcourse it is not working as I want to. The Ordering works on Question.Order, but I would also the
Questions.SubQuestions List to be ordered according to SubQuestions.Order

View 2 Replies

Removing Items From A List Box

Feb 23, 2011

I am trying to get a simple multi-page program to run correctly. I have everything set up, except for the "remove" function. When I try to remove an item from the list box nothing happens. Insted of trying to explain it I will show you the code...

Index

Code:

[code]....

View 5 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved