0

I have a select List that is supposed to default the value saved from the model. However, it is not selecting the default value (checked the inspect element). I am using similar code for SelectLists in other places that work correctly.

Here is the code that I have that isn't working:

var techs = new HashSet<Guid>(db.usersInRoles.Where(u => u.RoleId == new Guid(Properties.Settings.Default.TechID)).Select(u => u.UserId));
ViewBag.TechnicianId = from user in db.Users
        join tech in techs on user.UserId equals tech
        where user.Status == 1
        orderby user.FirstName
        select new { TechnicianId = user.UserId, FullName = user.FirstName + " " + user.LastName };

and the view:

<span style="font-weight:normal;">@Html.DropDownListFor(m => m.TechnicianId, new SelectList(ViewBag.TechnicianId, "TechnicianId", "FullName"))</span>

After finding this Question and this Question, I modifyied my view to be like this:

<span style="font-weight:normal;">@Html.DropDownListFor(m => m.TechnicianId, new SelectList(ViewBag.TechnicianId, "TechnicianId", "FullName", @Model.TechnicianId))</span>

Unfortunately it is still not getting the default value from the model. I don't want to have to change my model to do something that should be so simple (as suggested in one of those links).

Here is the code that is working (and is the same to me):

ViewBag.Category = db.Categories.Where(c => c.Status == 1);

and the view:

<span style="font-weight:normal;">@Html.DropDownListFor(m =>
m.CategoryId, new SelectList(ViewBag.Category, "CategoryId", "CategoryName"))</span>
Community
  • 1
  • 1
djblois
  • 963
  • 1
  • 17
  • 52
  • What do you mean by the select list is supposed to default to the value saved from the model? if you have multiple users with user.Status == 1 how do you know which one should be selected? – krilovich Dec 04 '15 at 19:36
  • Try a List instead of what you are currently doing - http://stackoverflow.com/questions/4833652/asp-net-mvc-with-drop-down-list-and-selectlistitem-assistance from the accepted answer.. that way you can define which item from the list you want to be selected by default – krilovich Dec 04 '15 at 19:40
  • @krilovich because the lambda expression in the view is selecting the user that is selected in the model – djblois Dec 04 '15 at 19:45
  • I don't think that will help me because the default value is not coming from the user table where I pull them from. It is from the model that the view is typed to. It is a help desk system for people to open up IT tickets and the model the view is typed to is Tickets so the default value is the person assigned to that ticket. – djblois Dec 04 '15 at 19:48
  • You cannot use the same name for the model property and the `SelectList`. Start by changing the `ViewBag` property to (say) `TechnicianList`. Note also you do not need to add the 4th parameter (`@Model.TechnicianId`) when binding to a model property (its completely ignored by the HtmlHelper method). If the value of `model.TechnicianId` matches the value of one of the options, then that option will be selected (unless you using this in a loop which you do not appear to be doing). –  Dec 04 '15 at 23:55
  • Thank you @StephenMuecke that was the answer. – djblois Dec 07 '15 at 14:20

1 Answers1

0

This is how you generally do it to render a drop-down and select an item as selected option.

public class CreateOrdeViewModel
{
    public List<SelectListItem> TechList {set;get;}
    public int SelectedTech {set;get;} 

    //Your other viewmodel properties also goes here
}

And, in your GET action method:

public ActionResult Edit(int id)
{
    var vm = new CreateOrderViewModel();
    var order = dbContext.Orders.FirstOrDefault(s=>s.Id==id);

    vm.TechList = GetItems();
    //This is where you set the selected value which you read from db
    vm.SelectedTech = order.SelectedTechId;

    return View(vm);
}

private List<SelectListItem> GetItems()
{
    // just for demo. You may change the LINQ query to get your data. 
    // Just make sure to return a List<SelectListItem>
    return db.Users.Select(s => new SelectListItem   
    { 
        Value = s.Id.ToString(),
        Text = s.Name
    }).ToList();
}

And, in your view:

@model CreateOrderViewModel
@using(Html.BeginForm())
{
    @Html.DropDownListFor(s => s.SelectedTech, Model.TechList,"s")
}
ataravati
  • 8,891
  • 9
  • 57
  • 89
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Thank you Shyju and Ataravati. However, what I am doing there works in two other places (in the same view). I just edited the op to provide one example. Can you tell me what the difference is between the two? – djblois Dec 04 '15 at 20:25