1

I have a stored procedure that retrieves existing data from the database and I want to set my DropDownListFor to a value that is equal to the data from the stored procedure's result. This is for an edit module.

Here is my code for my controller:

    [HttpPost]
    public ActionResult EditGetProjDetails(int id)
    {
        var vm = new ProjectsViewModel();

        sp_GetProjectDetails_Result result = db_RIRO.sp_GetProjectDetails(id).FirstOrDefault();

        vm.BusinessLineList = db_RIRO.sp_GetAllBusinessLine()
        .Select(a => new SelectListItem
        {
            Value = a.BusinessLineID.ToString(),
            Text = a.BusinessLine,
            Selected = // what code do i put here?
        })
        .ToList();

        vm.ProjectID = result.ProjectID;
        vm.BusinessUnit = result.BusinessUnit;

        return PartialView("~/Views/Project/_EditProjDetails.cshtml", vm);

This is my View:

 @Html.DropDownListFor(a => a.BusinessLineID, Model.BusinessLineList, new { @class = "form-control input-sm", placeholder = "Business Line", required = "required", autocomplete = "off" })

and here is my ViewModel:

    public int BusinessLineID { get; set; }
    public List<SelectListItem> BusinessLineList { get; set; }
    public string BusinessLine { get; set; }

Solution:

        var vm = new ProjectsViewModel()
        {
         BusinessLineID = 2
        };

        sp_GetProjectDetails_Result result = db_RIRO.sp_GetProjectDetails(id).FirstOrDefault();

        vm.BusinessLineList = db_RIRO.sp_GetAllBusinessLine()
        .Select(a => new SelectListItem
        {
            Value = a.BusinessLineID.ToString(),
            Text = a.BusinessLine
        })
        .ToList();

        return PartialView("~/Views/Project/_EditProjDetails.cshtml", vm);
Odie
  • 375
  • 4
  • 16
  • You do not set the `Selected` property. You set the value of `BusinessLineID` –  Jul 09 '18 at 21:58
  • @StephenMuecke Hi. Can you help me with this? The duplicate answer you have posted doesn't seem to use a stored procedure to retrieve data to a list. – Odie Jul 10 '18 at 09:47
  • A stored procedure has nothing what so ever to do with it (and we do not even know what it returns anyway). Read the dupe carefully. Its the value of `BusinessLineID` that determines what is selected. No where in your code have you even set a value for `BusinessLineID`, therefore the first option will be selected (because something has to be). If the value of `BusinessLineID` matches exactly the value of one of the options, then that option will be selected. –  Jul 10 '18 at 09:52
  • @StephenMuecke Please check new edit. I've set the `BusinessLineID` value on the ViewModel. How do I match the `BusinessLineID` on the `Select` to the value that I've set? – Odie Jul 10 '18 at 11:01
  • Sorry, I do not understand what you mean. If your `sp_GetAllBusinessLine()` contains a record with `BusinessLineID = 2`, then that option will be selected when the view is rendered –  Jul 10 '18 at 11:16
  • Yes it does contain that `BusinessLineID`. Do you mind creating a pseudo-code for reference? I saw in this in the dupe: `TipoviDepozita = new (yourCollection, "Id", "Naziv"), TipPopustaId = 2` What code should I put in the `yourCollection`? – Odie Jul 10 '18 at 12:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174720/discussion-between-stephen-muecke-and-odie). –  Jul 10 '18 at 12:09

1 Answers1

1

You are looping over all business lines, then creating a list of selectlist items with a value and text for each BusinessLine.

sp_GetProjectDetails_Result result has the single business line id you want selected?

If so, in your loop for each business line, set the select list item's selected property to true if the id is the same as result id from the procedure.

Something like:

 Selected = (a.BusinessLineID == result.ID) //or whatever result is from the procedure
L0uis
  • 703
  • 5
  • 8