0

i have following code : controller method:

 public ActionResult Register(int? registrationTypeId)
        {
            IEnumerable<AccountType> accountTypes = new List<AccountType>
            {
                new AccountType
                {
                    AccountTypeId = 1,
                    AccountTypeName = "Red"
                },
                new AccountType
                {
                    AccountTypeId = 2,
                    AccountTypeName = "Blue"
                }
            };
           // I want to select account type on registrationTypeId
            ViewBag.AccountTypes = accountTypes;
            return View();
      }

View

<div class="col-md-10">
            @Html.DropDownListFor(n => n.AccountType,
         new SelectList(ViewBag.AccountTypes, "AccountTypeId", "AccountTypeName"), new { @class = "form-control" })
</div>

Model

public class RegisterViewModel
    { 
        [Required]
        [Display(Name = "Account Type")]
        public int AccountType { get; set; 
    }

As you can see registrationTypeId in controller , i want to set the type on its bases if it is not null ,otherwise set to red. I have tried a alot but nothing worked for me. Any help will be appreciated !

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
Baqer Naqvi
  • 6,011
  • 3
  • 50
  • 68

2 Answers2

1

I would highly recommend that you don't pass your list through the view bag. have seen too many questions where that has caused major issues. add this to your model

public List<SelectListItem> AccountTypes { get; set; }

in your controller in the get method set your default and set your list

Model.AccountType = 1;  // change the one to your default value
Model.AccountTypes = accountTypes;  //instead of ViewBag.AccountTypes = accountTypes;

then on your view

@Html.DropDownListFor(x => x.AccountType, Model.AccountTypes)

setting AccountType before passing the model to the view will set the default and the selected value on the view will be passed back in that same value.

Matt Bodily
  • 6,403
  • 4
  • 29
  • 48
  • Why are lists passed to the view in the ViewBag in examples and in the out of the box project? – Inspector Squirrel Dec 05 '14 at 09:45
  • my guess is it is a quick and dirty way to get it to work. passing the list through the model is much more stable and reliable. – Matt Bodily Dec 05 '14 at 16:47
  • Microsoft's examples are famous for not being very industry-proof and for often showing "worst practices" unfortunately... because they are trying to market the idea of "easy to code" so they want it to look as simple as possible on their website – nothingisnecessary Mar 01 '17 at 19:39
0

The Wrong Way To Do This

var accountTypes = new SelectList(accountTypes, "AccountTypeId", "AccountTypeName");

foreach(var item in accountList)
    if (item.AccountTypeId == registrationTypeId)
        item.Selected = true;

ViewBag.AccountTypes = accountTypes;

In view,

@Html.DropDownListFor(n => n.AccountType, (SelectList)ViewBag.AccountTypes)
Inspector Squirrel
  • 2,548
  • 2
  • 27
  • 38
  • You do not (and should not) do this. Setting the `item.Selected` property is ignored by the `@Html.DropDownListFor()` so its pointless. If the value of `AccountType` matches the value of one of the options, then that option will be selected. –  Dec 04 '14 at 23:11
  • Didn't realise, never actually done it like this before. – Inspector Squirrel Dec 05 '14 at 09:43
  • 1
    I'll leave it up, your explanation might help people. – Inspector Squirrel Dec 05 '14 at 09:50