Posting variable data - asp.net

I am building an application in ASP.NET, C#, MVC3 and SQL Server 2008.
A form is presented to a user to fill out (name, email, address, etc). I would like to allow the admin of the application to add extra, dynamic questions to this form.
The amount of extra questions and the type of data returned will vary. For instance, the admin could add 0, 1 or more of the following types of questions:
Have you a full, clean driving liscence?
Rate your drivings skills from 1 to 5.
Describe the last time you went on a long journey?
etc ...
Note, that the answers provided could be binary (Q.1), integer (Q.2) or free text (Q.3).
How would I allow the posting of 0, 1 or more random values from the form back to my code?
Any help would be greatly appriecated.
Thanks in advance.
PS. I asked a similar question regarding the storing of this data in sql, see here

You will need to use an EAV scheme for storage. For a discussion of the topic, along with common pitfalls and solutions, see Best Practices for Semantic Data Modeling for Performance and Scalability. Other alternatives are Sparse Columns and XML data type column, but I think EAV is superior most of the times.

You'll have to accomplish this using dynamic controls.
Basically you'll read the question list from your database, inject zero or more custom controls to emit the question(s) and accept the answer(s). Probably wrapped in a repeater.
On post back you'll have to rehydrate the dynamic controls so that you can get the values.

I have done this before when I worked on a survey tool.
So first of all you need to decide what questions type you'll have. I had SingleOption, MultiOption and FreeText type questions.
User logs into the admin screen and selects a type of question he wants to add. He then clicks on Add button, which submits a request to a controller/web service.
Controller looks at type of question and adds a record into the database.
Rough table structure below:
QuestionID | SurveyId | QuestionTypeId
2 4 1
This is your basic structure.
When you generate an interface for answering questions, you will retrieve all questions with a SurveyId = 4.
You will then write an HTML helper that looks at question type and generates an appropriate control.
When it comes to submitting a survey, or answers to your question, you will display all questions and answer fields in some div, say we call it "survey_questions". Inside this div will be a form which will contain all your questions and controls for answers (text inputs, checkboxes etc).
You will then submit this form through jQuery to a controller/web service. Controller/Web Service will read survey id and it will iterate through all submitted values.
You will then use these values to populate a table for storing survey results. I understand that this is very brief and abstract, but I have worked over a month on this, so if this is useful and you need further help, then please ask.

I found a great article that solves my problem.
You can find it here

Related

Dataobject and Page relationships

http://www.silverstripe.org/archive/show/1638
The above post seems like it's what I should do but I just need some help sorting this out in my head.
Firstly, I need to create a relationship between a page (Owner, for example) and a dataobject (Car). An owner can create many cars which are linked to that one owner. However, I have another page (Garage) which can create cars that are linked to every owner. If an owner does not want one of these cars they reject it. I was thinking the manymanydataobjectmanager would be good for that bit.
Each owner should only be able to see the cars that relate directly to them within the CMS, not other peoples cars, so I was using dataobjectmanager and assigning permissions to the page using groups.
The thing that is really making this awkward is that when it's all set up I need to output JSON which will consist of the cars the owners created and the cars they accepted from the garage, not the ones they rejected. I'm thinking I need another table like the linked table but with a status column perhaps?
To clarify, my question is how do I create this mess in a constructive SilverStripe way? Is the approach I was taking correct or is there a better way?
Many thanks in advance and please tell me if I've been unclear.
are you using silverstripe 3?
could you clarify what of the actions happen in the backend and what actions are possible for the user in the frontend?
maybe for your relations it could be better to use ModelAdmin:
http://doc.silverstripe.org/framework/en/reference/modeladmin
It gives you the opportunity to manage relations without the Sitetree/Pages Overhead. For example creating a Sitetree Element just to have an Owner is not the best way - except if you really need an Owner represented as a real Page. Owner could be also just a Dataobject instead.
Especially if you want to output just JSON in the end you are maybe completely independent of Sitetree/Pages... then you could write a custom controller with a routing rule and which gives you back just the data that you need:
http://doc.silverstripe.org/framework/en/topics/controller
regards,
Florian

Separate ASP.NET MVC View object for each Model CRUD operation?

Most MVC tutorials I've been reading seem to create 4 View objects for each Model. For example, if my Model is "Foo", there seem to be 4 .cshtml files: Foo/Create, Foo/Delete, Foo/Details, and Foo/Edit. Using the VisualStudio "scaffolding" helper does this as well.
Is this really considered MVC best-practice? It just feels wrong to have 4 classes that are 80-90% identical to each other. When I add a new field to Foo, I need to edit all 4 .cshtml files. This sort of dual-maintenance (quad-maintenance?) just makes my OO skin crawl.
Please tell me: is there an expected/accepted best-practice which handles this differently? Or, if this really IS accepted best-practice, tell me why the quad-maintance shouldn't make me squirm.
I'm a reasonably skilled veteran of ASP.NET / c# / OO Design, but pretty new to MVC; so apologies if this is a noob question. Thanks in advance for your help!
Edit: thanks for all the replies! I marked the most thorough one as the answer, but upvoted all that were helpful.
You'll probably need between two and four different views:
List (for viewing many things)
View (for viewing a single thing. Might not be necessary, if it's OK to use Edit as View, or if List has room to show all properties)
Create
Edit (can be the same as create, if you code cleverly)
Thus, if your model doesn't have too many properties to show them all in a table, and if you're OK with not having a static, non-editable view for just examining, you can get well away with just List and Edit, and scrap the other two.
However, this doesn't solve your problem of double (or triple) maintenance if you update your model. There's other magic for that ;)
In ASP.NET MVC 3, there are extensions on HtmlHelper that let you do Html.DisplayForModel() and Html.EditorForModel(). These use predefined templates to nest themselves into your object and draw up display/edit fields for all public properites. If you pass DisplayForModel an IEnumerable<Foo>, it will create a table with column headers that are the property names of Foo (using the DisplayName attribute information if you supplied it) and where each row represent one Foo instance. If you give EditorForModel a Foo, it will create a <label> and an <input> for each public property on Foo.
All of the templates used by these powerful extension methods can be replaced by you, if you're not happy with the defaults. This can be done either on the level of Foo, in which case you'd be back in your double-maintenance scenario, or on lower levels (such as string or DateTime) to affect all editor/display fields generated with the templates.
For more information on exactly how this works, google "ASP.NET MVC 3 editor templates" and read a couple of tutorials. They'll explain the details much better than I could.
The views that ASP.NET MVC create for you don't necessarily need to be the views that you use in production. I found those just to be handy while developing quick prototypes or to test the database CRUD operations. Feel free to create whatever view(s) you would like to handle the operations.
I would generally just have 1 or 2 views to handle the basic operations and not use the built in views that are generated. For example, 1 view for adding, editing, or details and 1 view to show a list of objects.
It all depends on your application.
If you have a single item, you don't need a List view. If you can't edit it, then you don't need an edit view. Create and Edit can often be the same view, unless there are special things you need to do in one, but not the other.
In other words, use as many views as you need. There's no hard and fast rule here. The scaffolding is just there to help you on your way. Many kinds of apps will work just fine using the scaffolding, and won't require advanced HTML or Javascript.
Why would you want multiple views? Well, let's take the display and edit functions. You could create one view, in which you use if statements to determine the edit mode of the view, however this will complicated the view logic and views should be as simple as possible.
The reason to create seperate views is that its easier to maintain than one gigantic view with tons of conditional logic in it.
You can use exactly the same view when you are performing [HttpGet]. Given that you pass a proper ViewModel to this view, it will populate with appropriate data every time whether you are loading create, update, or delete Action.
The problem becomes apparent when you try to post that data to a specific Action.
Naturally View should have only one form, which will be used for posting data. When you declare this form, you specify which exactly Action to use for Post.
Having 3 different Submit buttons in that form will not make a difference since all of them will post the same form to the same Action.
You could do some javascript tweaking on OnClick event for these buttons to change Action to which data is posted, but this definitively would not be best practice.
Buttom line: having 4 different views for each of the CRUD actions is the best practice for MVC.
I tend to create the following for an object's CRUD ops:
index
_form (partial)
new
update
delete
view
As the same form is shared between new and update, there is very little difference between the two. It really depends on how much you want the variation to be, honestly.
As for delete, this is optional. I like to have a view in case javascript is disabled.
edit:
You mention view models and the guy above posted a long, convoluted (no offense) VM code sample.
Personally, I hate classes written to basically mirror domain objects and are only used to "move" data. I hate VMs. I hate DTOs. I hate everything that makes me have to write more code than is necessary.
I guess I've drank the coolaid of other frameworks (rails, sinatra, node.js) to the point where I can't stomach the idea of tossing DRY to the wind.
I personally say skip um.
Edit2 I forgot list..

How to build flexible web forms in ASP.NET

I'm using asp.net and I need to build an application where we can easily create forms without recreating the database, and preferably without changing the create/read/update/delete queries. The goal is to allow customers to create their own forms with dropdowns, textboxes, checkboxes, even many-to-one relationship to another simple form (that's stretching it). The user does not have to be able to create the forms themselves, but I don't want to be adding tables, fields, queries, web page, etc. each time a new form is requested/modified.
2 questions:
1) How do I structure a flexible database to do this (in SQL Server)? I can think of two ways: a) Create a table for each datatype (int, varchar(x), smalldatetime, bit, etc). This would be very difficult to create the adequate queries. b) Create the form table with lots of extra fields and various datatypes in case the user needs 5 integers or 5 date fields. This seems the easiest, but is probably pretty inefficient use of space.
2) How do I build the forms? I thought about creating an xml sheet that had the validations, data type, control to display, etc. as a list. Then I would parse through the xml to build the form on the fly. Probably using css to do the layout (that would have to be manual, which is ok).
Is there a better/best way? Is there something out there that I could look at to get ideas? Any help is much appreciated.
This sounds like a potential candidate for an InfoPath solution. At first blush, it will do most/all of what you are asking.
This article gives a brief overview of creating an InfoPath form that is based on a SQL data source.
http://office.microsoft.com/en-us/infopath-help/design-a-form-template-based-on-a-microsoft-sql-server-database-HP010086639.aspx
I have built a completely custom solution like you are describing, and if I ever did it again I would probably opt for either 1) a third-party product or 2) less functionality. You can spend 90% of your time working on 10% of the feature set.
EDIT: reread your questions and here is additional feedback.
1 - Flexible data structure: A couple things to keep in mind are performance and the ability to write reports against the data. The more generic the data structure, the harder these will be to achieve (again, speaking from experience).
Somewhat contrary to both performance and report-readiness, Microsoft SharePoint uses XML fragments/documents in generic tables for maximum flexibility. I can't argue with the features of SharePoint, so this does get the job done and greatly simplifies the data structure. XML will perform well when done correctly, but most people will find it more difficult to write queries against XML. Even though XML is a "first class citizen" to SQL Server, it may or may not perform as well as an optimized table structure.
2 - Forms: I have implemented custom forms using XML transformed by XSLT. XML is often a good choice for storing form structure; XSLT is a monster unto itself, but it is very powerful. For what it's worth, InfoPath stores its form structure as XML.
I've also implemented dynamic forms using custom controls in .Net. This is a very object-oriented approach, but (depending on the complexity of the UI) can require a significant amount of code.
Lastly (again using SharePoint as an example), Microsoft implemented horrendously complicated XML list/form definitions in SharePoint 2007. The complexity defeats many of the benefits. In other words, if you go the XML route, make your structures clean and simple or you will have a maintenance nightmare on your hands.
EDIT #2: In reference to Scott's question below, here's a high-level data structure that will avoid duplicated data and doesn't rely on XML for the majority of the form definition.
Caveat: I just put this design together in SQL Management Studio...I only spent 10 minutes on it; developing a flexible form system is not a trivial task, so this is an over-simplification. It does not address the storage of user-entered data, just the definition of the form.
The tables:
Form - top-level form table which contains (as you would guess) the collection of fields that comprise the form.
Field - generic fields that could be reused across forms. For example, you don't want 50 different "Last Name" fields for 50 different forms. Note the "DataTypeId" column. You could store any type you wanted in this column, like "number, "free text", even a value that indicates the user should pick from a list.
FormField - allows a form to contain 0-many fields in its definition. You could even extend this table to indicate that the user can ADD as many of this field as they need.
Constraint - basically a lookup table that defines a constraint type (maybe it's max length, max occurrences, required, etc.)
FormFieldConstraint - relates a constraint to a particular instance of a form field. This table combines a specific form with a specific field with a specific constraint. Note the metadata column; this potentially would be a good use for XML to store the specifics of the constraint.
Essentially, I suggest building a normalized database with few or no null values and no duplicated data. A structure as I've described would get you on the path to that goal.
I think if you need truly dynamic forms saved into a database, you'd have to create a sort of "dictionary" data table.
For example...
UserForms
---------
FormID
FieldName
FieldValue
FormID relates back to the parent form (so you can aggregate all of the fields for one form. FieldName is the name of the text field entered from. FieldValue is the value entered/selected for that field.
Obviously this isn't a perfect solution. You could have issues typing your data dynamically, but I leave the implementation of that up to you.
Anyways, hopefully this gives you somewhere to start thinking about how you'd like to accomplish things. Good luck!
P.S. I've found using webforms with .NET to be a total pain when doing dynamic form generation. In the instances I had to do it, I ditched it almost entirely and used native HTML elements. Then rewired my form by using the necessary values from Request. Probably not a perfect solution either, but it worked the best for me.
We created a forms system like the one you're describing with a schema very similar to the one at the end of Tim's post. It has been pretty complicated, and we really had to wrestle with the standard WebForms controls like the DetailsView and GridView to make them be able to perform CRUD operations on groups of answers. They're used to binding straight to properties on an object, and we're forcing them to look up a field ID in a dictionary first. They don't like it. You may want to consider using MVC.
One tricky question is how to store the answers. We ended up using a table that's keyed on FieldId, InstanceId (for example, if 10 people are filling out your form, there are 10 instances), and RowNumber (because we support multi-row forms for things like employment history). Instead of doing it this way, I would recommend making your AnswerRow a first-class concept with its own table tied to an Instance, and having the answers be linked to the AnswerRow and Field.
In order to support different value types, we had to create multiple answer fields on our answer table (AnswerChar, AnswerDate, AnswerInt, AnswerDecimal). Each of these maps to one or more Field Types (Date, Time, Number, etc.). Each Field Type has its own logic to represent the value in the UI, and put the value into the appropriate property on our answer objects.
All in all, it's been a lot of work. It's worth it for us, since this is the crux of the product, but you will definitely want to keep the cost in mind before embarking on a project like this.

Architecture advice for a website

I'm looking for a bit of advice with regards to the structure of an application I'm tasked with.
I need to have a series of webforms to store some data, most of the data is the same but each form would have some differeing data depending on the form used.
Would a base class then a series of classes inheriting from this for the specific data be best to use?
As in:
order - compAOrder
- compBOrder
- compCOrder
How about strucuting the database for this sort of application structure?
The answer to this kind of question is allways "It depends" but here is a possible solution:
Program the system with a single web page for form data input.
Use a query string value to identify which form it is.
Hide and enable fields depending upon which form it is.
Have a single data table that has all the possible fields.
Hope this helps
Shiraz
I eventually went with a catch-all table with all the fields and different forms.

Good pattern or technique for webforms with 100s of fields [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
At my job, I have to implement web forms for loan applications with sometimes up to a hundred different input fields, and then save the application into the database for later retrieval.
The person whom I replaced created a sql table with 100s of columns where each row represents a loan application and there is a column for every field.
The problem with this is that I find myself having to type out the 100 fields a bunch of times, getting data from form, saving to database, retrieving from database, writing to output webform.
And then whenever there is a change to the application, I have to make the change in quite a few places.
So it can not only be cumbersome but error prone.
Is there a good design pattern that handles this?
See if you can come up with a better table design. 100 Cols is just too much IMO. If not you could use dynamic data. Also look for a better UI pattern or break it into more than 1 pages, looking at 100 fields to fill in could be over whelming for the users.
Code generation is the best bet. MyGeneration and CodeSmith are two very good tools.
I've had very similar projects and I used both generation tools and prefer CodeSmith.
100's of fields in a form is too many. Try to aim for max 20 per page.
If you can, try to isolate each part of this form into a separate page, and even better, into a wizard of some kind.
e.g. (example steps)
Step 1: Client Profile (name, age, SSN, etc.)
Step 2: Loan Data (Fixed, Variable, Amount, Terms, Special Deals...)
Step 3: Credit Check?, Pre-Approval?
Step 4: Variations/Calculations...
If you organize the data in structured groups, you should be able to make each portion work independently (e.g. you can input basic loan data, and get some preliminary numbers without needing to enter client data (yet).
More importantly, if the user enters a value in one of the 100 fields that causes the backend to not store (e.g. value out of range) what happens? does the user lose all the data they entered? - for usability make sure that nothing is lost when the user attempts to save/move to the next step.
Perhaps ASP.NET Dynamic Data would work for you. It is described as "a framework that enables you to quickly build a functional data-driven application"
I've worked with these loan applications before and I'll tell you straight out that your data model is broken. Are you gonna make a new row in the BigTable when a borrower comes in for a refi? What if the deal falls through but they want to try again for a different house? If you just edit the data in the row for that application then you're losing valuable metrics on your % of closed deals.
First, fix your data model. Then, embrace LINQ if at all possible.
With respect to the UI, I'd either do it as a wizard (which is pretty close to how these apps are filled out) or build it as one big form that the user scrolls through and adds stuff to (use AJAX in each section so that the user can save as they go along).
You also need to consider off the shelf solutions for this stuff. Don't reinvent the wheel here; these loan apps don't change often enough to throw a full time developer at them.
Edit: Sorry I was assuming that this was for residential mortgages. I don't know if that's true, but I still think your data model is broken. If you have the ability to change it you should.
Design pattern? But of course: The Adaptive Object-Model.
One thing that you can do is programmatically take the column names and put them on a file in the format you need. That will be to solve your immediate pain. Below is a piece of c# code that will get the column name from a oledbreader object.
internal List<string> GetFieldList(string sTableName)
{
tableName = sTableName;
BuildQuery("*");
command = SetupConnection();
List<string> FieldList = new List<string>();
OleDbDataReader reader = command.ExecuteReader();
for (int i = 0; i < reader.FieldCount; i++)
FieldList.Add(reader.GetName(i).ToString());
return FieldList;
}
To solve the situation in the long run, you probably want to do store procedures or functions with the fields as parameteres. you still have to put the values in each of the parameters, but is going to easy the management of each
he problem with this is that I find myself having to type out the 100 fields a bunch of times, getting data from form, saving to database, retrieving from database, writing to output webform.
hope it helps!!!
Ouch.
Is LINQ available to you? What version of .NET are we talking about here?
BTW, if it isn't obvious already, from a GUI perspective I suggest you hide most of these fields from the end user and reveal them in sections as the user progresses, which has the advantage that you can avoid showing what doesn't need to be and validate as you go.
And with some sort of progress indicator so they know there's an end in site (ha ha).
I've had issues like this before, and know there may be valid reasons for having so many columns. It's rare, but it can happen.
What you might want to do, is create a partial class in it's own file which contains all the data field code. This might be fields, properties, and possibly method parameters, and/or just your CRUD functionality. And generate that partial class, this will help ease your future maintanence.
Good Luck.

Resources