简体   繁体   中英

Creating Entities outside DetailsView or FormView

Using EntityFramework, I have created a page with a number of DetailsView and FormView controls to try and capture survey information (100% inserting; no updating or deleting) to be saved to sql server. The tables/entities involved include Survey, Question, Respondent, Response, Answer and a couple of Type lookups. (A Survey has a list of Questions and is completed by a Respondent. The Respondent's Response has a list of Answers. An Answer answers a Question.) Hopefully that all makes sense.

Rather than have the user click the autogenerated Insert button in each DetailsView or FormView, I would like to provide a single submit button to insert all the new data, which is what I am currently - and unsuccessfully - trying to code. Essentially, i'm looking for a way to invoke the command called by autmatically created Insert button for each *Form.

I figured the way to do this is to loop through the DetailsView and FormView controls, saving (attaching?) the entities as I go along to the ObjectContext, and then calling SaveChanges() to get them all inserted into the db at once. However, I'm having a problem accessing the new entities. I don't seem to be able to access them through the FormView/DetailsView itself, and I can't seem to access the BoundFields within the *Views.

I tried to nest FormViews inside FormViews as this infers it is possible, but I get the error Type 'System.Web.UI.WebControls.FormView' does not have a public property named 'FormView'.

I'm about to start looking into solutions involving the ObjectContext and/or EntityDataSource s tied to the DetailsView s, but it seems to me that if I'm coming at it from that side, then I might as well use plain TextBox es and just loop through them creating all the entity stuff in code, rather than letting the databinding features kick in.

Can someone point to how I can run each *Form's insert button command to save all the newly created entities in each DetailsView/FormView or let me know if I should start looking in another area (eg ObjectContext or EntityDataSource)?

Many thanks
mcalex

I'm an idiot.

I just needed to call the *Form's InsertItem method.

Have no idea why I didn't realise the first time i read the API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM