Ok, I've read Phil Haack's
article on binding to a list and I've got that working fine on one view. But what I'm stuck when doing it off a master record. Everything seems to work except that when I look in the database the detail records previous associated
with the current master have their Foriegn key to master set to Null and a new set of detail records have been inserted.
I've got a really simple form for this object
public class Master
{
public int ID { get; set; }
public string MasterTitle { get; set; }
public virtual IList<Detail> Details { get; set; }
}
public class Detail
{
public int ID { get; set; }
public string DetailName { get; set; }
public virtual Master Master { get; set; }
}
The form collection comes back with the expected prefixes:
And the Controller.UpdateModel(master) binds all the properties correctly. But when I call dbContext.SaveChanges it issues the follow sql from sql profiler (psuedo code)
update detail1 set masterID = null
update detail2 set masterID = null
update master set masterName = 'newname'
insert detail1 ...
insert detail2 ...
I've got a work around that works but it's pretty hackish and I'm currently not matching up the keys so it's dependent on everything coming back in the right order. Plus I've got to include all the fields that I want updated.
public ActionResult Edit(FormCollection collection)
{
try
{
using (var ctx = new PlayContext())
{
var id = int.Parse(collection["ID"]);
Master master = ctx.Master.Find(id);
UpdateModel(master, new [] {"MasterTitle"});
for (int i = 0; i < master.details.Count; i++)
{
UpdateModel(master.details[i], "Details[" + i + "]", new[] { "DetailName" });
}
ctx.SaveChanges();
return View(master);
}
}
catch (Exception e)
{
ModelState.AddModelError("", e);
}
return View();
}
I thought that entity framework's code first was the culprite but I've unit test that and it works fine.
I've got a feeling that UpdateModel is somehow removing and re-adding the children.
Has anyone else got this to work? Of course, I could throw in the towel and parse the indexed field names myself, but I'm so close!
I have
blogged about creating master detail form using asp.net mvc where you can add n child records on clietn side without the need of sending ajax request just to bring the editor fields for child records. it used jquery templates
I understand what you mean, form the UI point of view, that is allowing, add/delete/edit of a list. In other terms....I kind of datagrid.
The main problem here was reflecting all operations with the db, anyway.
Give a look also to my Mvc Controls Toolkit. There I offer several alternatives datagrid server controls, to client side templates plus Client-side ViewModel and bindings.
@francesco Yes i have looked at your controls a while back and i found them a little overkill for my scenrio. i just wanted to create a master detail form on sigle UI and hated to send an ajax call just to bring the editor for the detail record. I have just
brought that template on client side and calling it using jquery using/ammending nice Html helper in Steve's demo proejct on
editing variable length list in mvc style.
@adeel.zahid I understand. Ajax calls are for the case where each detail record has several more data than the simple "short view" of the item. Client side templates are a more attractive alternative when the detail record is small enough for you to send
all data once and for all when the page is rendered.
While this can be done with just the help of jquery templates, knockout library helps a lot to do this in a very flexible way while implementing a cool rich ui. See here:
I built an enhancement to knokcout library for Mvc, that allow you to transfer templates done with Razor Helpers to the client as clint-side templates. See here:
@franceso thanks for sharing such a helpful material and valuable contribution to the society. However, as i said earlier, incorporating knockout or mvc controls toolkit will be overkill for my situation (for this particular case only) because i m already
using telerik controls in my projects and changing it at this time may not be possible. But, i would be interested in seeing how you do client side validation with mvc controls toolkit and if it is possible to create a new order form with all order Lines on
the same page using mvc controls toolkit ( Any tutorial on these features will be higly valuable so i could plan using this product in my next project).
i would be interested in seeing how you do client side validation with mvc controls toolkit
You have just to put validation attriibutes on your classes. All Mvc Controls and helper are fully compatible with both the server side and the client side Mvc 3 and Mvc validation mechanism(they support both old Microsoft validation and unobtrusive validation).
Also if you use client-side templates your input fields (that can be also server controls) your input will be validated on both client side and server side based on the validation attributes you placed on the classes. You don't need to write any code to
have validation only Validation attribute.
Mvc Controls Toolkit engine do all job for you. ALL Mvc Controls toolkit tutorials show some client side validation. The list of all tutorials is here:
...... and if it is possible to create a new order form with all order Lines on the same page using mvc controls toolkit ( Any tutorial on these features will be higly valuable so i could plan using this product in my next project).
Not sure what you mean....Mvc Controls Toolkit has two kind of datagrid you can use to show edit/insert/delete data items.
If you refer to the detail form of my Mvc Controls toolkit it can beshowed also in the same page(not as a new window) just changing removing some parameters that define the new window(see
here for a tutorial on this)...however it is always based on Ajax.
However i am nor sure this is what you want...please explain me better
However i am nor sure this is what you want...please explain me better
i want to go to server side (i.e submit the form) when i have completed data entry for both master and detail portion of the form. Please download the demo code
from. it will give you clear idea of what i want.
danieleli
Member
6 Points
7 Posts
MVC 3 Master / Detail view inserts new detail records instead of updating existing records on con...
Feb 06, 2011 09:14 PM|LINK
Ok, I've read Phil Haack's article on binding to a list and I've got that working fine on one view. But what I'm stuck when doing it off a master record. Everything seems to work except that when I look in the database the detail records previous associated with the current master have their Foriegn key to master set to Null and a new set of detail records have been inserted.
I've got a really simple form for this object
The form collection comes back with the expected prefixes:
And the Controller.UpdateModel(master) binds all the properties correctly. But when I call dbContext.SaveChanges it issues the follow sql from sql profiler (psuedo code)
I've got a work around that works but it's pretty hackish and I'm currently not matching up the keys so it's dependent on everything coming back in the right order. Plus I've got to include all the fields that I want updated.
I thought that entity framework's code first was the culprite but I've unit test that and it works fine.
I've got a feeling that UpdateModel is somehow removing and re-adding the children.
Has anyone else got this to work? Of course, I could throw in the towel and parse the indexed field names myself, but I'm so close!
updatemodel parent child entity framework
ignatandrei
All-Star
137706 Points
22159 Posts
Moderator
MVP
Re: MVC 3 Master / Detail view inserts new detail records instead of updating existing records on...
Feb 06, 2011 09:22 PM|LINK
when you call Find, do you load the master with all details?( see include in EF )
francesco ab...
All-Star
20944 Points
3286 Posts
Re: MVC 3 Master / Detail view inserts new detail records instead of updating existing records on...
Feb 07, 2011 08:42 AM|LINK
Your problem has nothing to do with UpdateModel that works fine but with the changes tracking of EF.
Since this is a web application when you update your models you cannot use the same context you used to retrieve them, so
the newly generated context isn't able to compute the changes form the original state of the objects.
To solve, either you retrieve again your objects and just update the newly retrieved objects as YOU HAVE DONE in what you call an hackish solution,
or simply manage manually the state changes with the help of the Context.ObjectStateManager object.
If you are interested in the second solution (the other solution is the one you have already implemented)
pls see mya blog post: http://www.dotnet-programming.com/post/2010/10/30/Defining-MVC-Controls-2-Using-the-DataGrid.aspx
There I pass to the DB a list of objects that have been uodated with a DataGrid, some of them needs to be inserted other updated, and other deleted.
This way you can see for each different operation how to set the .ObjectStateManager object.
Mvc Controls Toolkit | Data Moving Plug-in Videos
adeel.zahid
Member
43 Points
34 Posts
Re: MVC 3 Master / Detail view inserts new detail records instead of updating existing records on...
May 24, 2011 08:05 PM|LINK
I have blogged about creating master detail form using asp.net mvc where you can add n child records on clietn side without the need of sending ajax request just to bring the editor fields for child records. it used jquery templates
francesco ab...
All-Star
20944 Points
3286 Posts
Re: MVC 3 Master / Detail view inserts new detail records instead of updating existing records on...
May 24, 2011 11:28 PM|LINK
@adeel.zahid
I understand what you mean, form the UI point of view, that is allowing, add/delete/edit of a list. In other terms....I kind of datagrid.
The main problem here was reflecting all operations with the db, anyway.
Give a look also to my Mvc Controls Toolkit. There I offer several alternatives datagrid server controls, to client side templates plus Client-side ViewModel and bindings.
Mvc Controls Toolkit | Data Moving Plug-in Videos
adeel.zahid
Member
43 Points
34 Posts
Re: MVC 3 Master / Detail view inserts new detail records instead of updating existing records on...
May 25, 2011 07:30 AM|LINK
@francesco Yes i have looked at your controls a while back and i found them a little overkill for my scenrio. i just wanted to create a master detail form on sigle UI and hated to send an ajax call just to bring the editor for the detail record. I have just brought that template on client side and calling it using jquery using/ammending nice Html helper in Steve's demo proejct on editing variable length list in mvc style.
francesco ab...
All-Star
20944 Points
3286 Posts
Re: MVC 3 Master / Detail view inserts new detail records instead of updating existing records on...
May 25, 2011 09:15 AM|LINK
@adeel.zahid I understand. Ajax calls are for the case where each detail record has several more data than the simple "short view" of the item. Client side templates are a more attractive alternative when the detail record is small enough for you to send all data once and for all when the page is rendered.
While this can be done with just the help of jquery templates, knockout library helps a lot to do this in a very flexible way while implementing a cool rich ui. See here:
http://knockoutjs.com/examples/gridEditor.html and also all other templating examples.
I built an enhancement to knokcout library for Mvc, that allow you to transfer templates done with Razor Helpers to the client as clint-side templates. See here:
http://mvccontrolstoolkit.codeplex.com/wikipage?title=Client-Side%20ViewModel%20%26%20Bindings
for an introduction to client side bindings, and here
http://mvccontrolstoolkit.codeplex.com/wikipage?title=Client-Side%20Templates
for templates specifically.
I have also tutorials on my blog:
http://www.dotnet-programming.com/post/2011/05/02/Low-BandWidth-Transfers-with-Client-Side-Templates-of-the-Mvc-Controls-Toolkit.aspx
http://www.dotnet-programming.com/post/2011/05/08/Handling-Big-Amounts-of-Data-with-Client-Side-Templates.aspx
Mvc Controls Toolkit | Data Moving Plug-in Videos
adeel.zahid
Member
43 Points
34 Posts
Re: MVC 3 Master / Detail view inserts new detail records instead of updating existing records on...
May 25, 2011 02:56 PM|LINK
@franceso thanks for sharing such a helpful material and valuable contribution to the society. However, as i said earlier, incorporating knockout or mvc controls toolkit will be overkill for my situation (for this particular case only) because i m already using telerik controls in my projects and changing it at this time may not be possible. But, i would be interested in seeing how you do client side validation with mvc controls toolkit and if it is possible to create a new order form with all order Lines on the same page using mvc controls toolkit ( Any tutorial on these features will be higly valuable so i could plan using this product in my next project).
thanks & regards
francesco ab...
All-Star
20944 Points
3286 Posts
Re: MVC 3 Master / Detail view inserts new detail records instead of updating existing records on...
May 25, 2011 09:04 PM|LINK
You have just to put validation attriibutes on your classes. All Mvc Controls and helper are fully compatible with both the server side and the client side Mvc 3 and Mvc validation mechanism(they support both old Microsoft validation and unobtrusive validation).
Also if you use client-side templates your input fields (that can be also server controls) your input will be validated on both client side and server side based on the validation attributes you placed on the classes. You don't need to write any code to have validation only Validation attribute.
Mvc Controls Toolkit engine do all job for you. ALL Mvc Controls toolkit tutorials show some client side validation. The list of all tutorials is here:
http://mvccontrolstoolkit.codeplex.com/wikipage?title=Tutorials
Not sure what you mean....Mvc Controls Toolkit has two kind of datagrid you can use to show edit/insert/delete data items.
If you refer to the detail form of my Mvc Controls toolkit it can beshowed also in the same page(not as a new window) just changing removing some parameters that define the new window(see here for a tutorial on this)...however it is always based on Ajax.
However i am nor sure this is what you want...please explain me better
Mvc Controls Toolkit | Data Moving Plug-in Videos
adeel.zahid
Member
43 Points
34 Posts
Re: MVC 3 Master / Detail view inserts new detail records instead of updating existing records on...
May 26, 2011 04:57 AM|LINK
@Francesco thanks for providing list of tutorials
i want to go to server side (i.e submit the form) when i have completed data entry for both master and detail portion of the form. Please download the demo code from. it will give you clear idea of what i want.
thansk and regards