1 Replies - 5125 Views - Last Post: 21 December 2013 - 09:22 AM

#1 sky2high  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-December 13

Generate dropdown/checkbox from model folder filename.cs (mvc4)

Posted 17 December 2013 - 01:18 AM

Currently writing a class file for a survey that is going to be used by the entity framework 5 to generate the controller and view. Questions are fixed which the users must answer all the questions.

sample code
[Key]
        public int UserId { get; set; }
        [Required]
        [Display(Name = "Gender :")]
        public int gender { get; set; }
        [Required]
        [Range(16, 50)]
        [Display(Name = "Age :")]
        public int Age { get; set; }
        [Required]
        [Display(Name = "Date of Birth :")]
        public DateTime DOB { get; set; }
        [Required]
        [Display(Name = "Postal Code :")]
        public int postalCode { get; set; }


What is the code to add a dropdown to gender such as it only have the value for male and female?

This post has been edited by Atli: 17 December 2013 - 03:13 AM
Reason for edit:: Use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: Generate dropdown/checkbox from model folder filename.cs (mvc4)

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Generate dropdown/checkbox from model folder filename.cs (mvc4)

Posted 21 December 2013 - 09:22 AM

This is a poorly written question, but I'll make an exception this time, and provide an answer.

Here is the short example of possible solution:
ViewModel:

    public class UserInfoViewModel
    {
        public UserInfoViewModel()
        {
            Genders = new List<SelectListItem>
            {
                new SelectListItem {Selected = false, Text = Gender.NotSelected.ToDisplayString(), Value = Gender.NotSelected.ToDisplayString()}, //if you change Value here to Gender.NotSelected.ToString() ModelState will be valid, but you'll see another validation error message
                new SelectListItem {Selected = false, Text = Gender.Male.ToDisplayString(), Value = Gender.Male.ToString()},
                new SelectListItem {Selected = false, Text = Gender.Female.ToDisplayString(), Value = Gender.Female.ToString()}
            };
        }

        public Gender Gender { get; set; }
        public List<SelectListItem> Genders { get; set; }
    }

    public enum Gender
    {
        NotSelected,
        Male,
        Female
    }

    public static class GenderEnumExtensions
    {
        public static string ToDisplayString(this Gender gender)
        {
            return gender == Gender.NotSelected ? "Not Selected" : gender.ToString();
        }
    }



Controller's actions:
        // GET: /Home/UserInfo
        public ActionResult UserInfo()
        {
            return View(new UserInfoViewModel());
        }

        [HttpPost]
        public ActionResult UserInfo(UserInfoViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return View(viewModel);
            }
            if(viewModel.Gender == Gender.NotSelected)
            {
                ModelState.AddModelError("Gender", "You must select gender!");
                return View(viewModel);
            }
            return View("Success", viewModel);
        }



UserInfo view:
@model Web.Models.UserInfoViewModel


<html>
    <head>
        <title>User Info</title>
    </head>
    <body>
        <div>
            @using (Html.BeginForm("UserInfo", "Home", FormMethod.Post))
            {
                <div>
                    @Html.DropDownListFor(x => x.Gender, @Model.Genders)
                    @Html.ValidationMessageFor(x => x.Gender)
                </div>
                
                <div>
                    <input type="submit" value="Submit form!" />
                </div>
            }
        </div>
    </body>
</html>



and Success view:
@model Web.Models.UserInfoViewModel

<h2>Success!</h2>

<div>@Model.Gender</div>


This post has been edited by lucky3: 21 December 2013 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1