开发者

Multi-View Search Form (ASP.NET MVC 2)

I have a requirement to build a "Search" form for a travel company. This search form will be used for searching different 开发者_如何学编程travel components such as Flights, Hotels, Cars etc.

Most of the fields in the form are common (such as travel dates, origin and destination cities etc) for all components but will also contain some component specific fields (such as Business/First/Economy Class & Nonstop Flight Indicators for Flight Search).

Even though most of the fields are common, they need to be displayed with component-specific labels...for example: "travel dates" would say "check-in & check-out dates" on Hotel search form where as they would say "departure & return dates" on Flight search form.

What is the best approach to design a search form which would display field with component-specific labels and also provides a way to map/associate fields to components (common fields to all component types and component-specific fields to corresponding component)? Is there something similar to Data Annotations for achieving this behavior?


So Within "Form A", field 1 has a certain display label A "Form" has many fields and each field has a display label that changes by form. A Form has one or more fields Not all forms need to have all fields (that is some forms may have fewer fields than the set of all fields).

So if you can imagine a set of tables in a database (the data from these tables can be cached in memory if need be) that represent this data then at the time of rendering a specific form, you'll get the set of fields and their display labels. It's easy enough to then bind the display labels to the appropriate components.

This is the best way to do it rather than rely on reflection.

The basic data structure would be a Dictionary that uses the form's name (or some other identifier) as the key. The "value" part of the Disctionary will be another dictionary that uses the control id/name as the key and the value will be the display label.

so something like:

Dictionary<string, Dictionary<string, string>>

Or you could have a "custom" dictionary for the latter like so

public class ControlDisplayLabelDictionary : Dictionary { }

And then you could populate the whole structure like so:

  var formControlLabels = new Dictionary<string,

ControlDisplayLabelDictionary>();

  var cdForFormA = new ControlDisplayLabelDictionary();

  cdForFormA.Add("Control1", "Travel Dates");
  cdForFormA.Add("Control2", "Some Label control2 on FormA");
  cdForFormA.Add("Control3", "Some Label control3 on FormA");

  formControlLabels.Add("Form A", cdForFormA);

  var cdForFormB = new ControlDisplayLabelDictionary();
  cdForFormA.Add("Control1", "Booking Dates");
  cdForFormA.Add("Control2", "Some Label control2 on FormB");
  cdForFormA.Add("Control3", "Some Label control3 on FormB");

  formControlLabels.Add("Form B", cdForFormA);

Of course, when the data comes from a database the actual population code will differ but the idea remains the same. Now you could choose the cache this structure in memory but I wouldn't do that unless there is a performance issues since the database will actually cache this "data" in memory anyway and it is very unlikely that the database engine will hit the hard disks after the first time. Provided of course you have enough memory on your database server box.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜