I am wanting to create a survey using ASP.NET MVC 3.0. Some questions will have radio buttons and some check boxes. I want to know the correct syntax for my View side as well as my ViewModel. I want to store my options for the answers in a Collection. Would IEnumerable be a good collection to use? Here is some of my ViewModel code.
[DisplayName("Country")]
[Required(ErrorMessage = "Country is required.")]
public string Country { get; set; }
public IEnumerable<string> Countries { get; set; }
[DisplayName("Business")]
[Required(ErrorMessage = "Please select a business unit.")]
public string BusinessUnit { get; set; }
public IEnumerable<string> Businesses { get; set; }
public Boolean ToGetFit { get; set; }
public Boolean ToChallengeMyself { get; set; }
public Boolean ToBeHealthier { get; set; }
public Boolean ChallengeOther { get; set; }
public string OtherString { get; set; }
public void build()
{
var myService = new SurveyService(new SurveyRepository());
Name = myService.getName();
Email = myService.getEmail();
}
What is the best way to get the information into my ViewModel when my build method is called? Should i be using IEnumerable or just strings?
here is my code on my .aspx page.
<li> What country do you live in? <br />
<%= Html.RadioButtonFor(model => model.Country, "Canada", true) %> Ecuador<br />
<%= Html.RadioButtonFor(model => model.Country, "Chile", true) %> Ghana <br />
<%= Html.RadioButtonFor(model => model.Country, "Italy", true) %> Nigeria <br />
<%= Html.RadioButtonFor(model => model.Country, "Germany", true) %> Romania
</li>
<li> What business unit do you work in?<br />
<%= Html.RadioButtonFor(model => model.BusinessUnit, "Pharmaceuticals", true ) %> Pharmaceuticals <br />
<%= Html.RadioButtonFor(model => model.BusinessUnit, "Mechanics", true) %> Vaccines <br />
<%= Html.RadioButtonFor(model => model.BusinessUnit, "R&D") %> R&D <br />
<%= Html.RadioButtonFor(model => model.BusinessUnit, "Distribution", true) %> Distribution <br />
</li>
<li> Why do you want to take part in this workout? <br />
<%= Html.CheckBoxFor(model => model.ToGetFit ) %> To get in shape <br />
<%= Html.CheckBoxFor(model => model.ToChallengeMyself ) %> To challenge myself <br />
<%= Html.CheckBoxFor(model => model.ToBeHealthier) %> To be healthier <br />
<%= Html.CheckBoxFor(model => model.ChallengeOther) %> Other
<%= Html.TextBoxFor(model => model.OtherString) %>
</li>
I am new to ASP.NET MVC but I have experience with both ASP.NET and the MVC pattern. I want as much emphasis placed on seperation of concerns as possible.
My controller class calls my build method. I have a Service class which grabs a Repository object which will be grabbing information from my Model/Database.
! I want my Radio buttons to be dynamically taken from my Database. And then if the user already has something selected from a previous session i want that to be found in my ViewModel and already selected when they load the page.