Home > Enterprise >  Set SelectedValue from ViewBag DotNet Core
Set SelectedValue from ViewBag DotNet Core

Time:09-17

I have the following model:

public class ActivityType
{
     [Key]
     public int ActivityTypeId { get; set; }
     public string ActivityTypeName { get; set; }
}

public class ActivitySubType
{
     [Key]
     public int ActivitySubTypeId { get; set; }
     public string ActivitySubTypeName { get; set; }
     [ForeignKey("ActivityType")]
     public int ActivityTypeId { get; set; }
     public virtual ActivityType ActivityType { get; set; }
}

public class ActivityRecord
{
     [Key]
     public int ActivityRecordId { get; set; }
     public string ActivityName  { get; set; }
     [ForeignKey("ActivitySubType")]
     public int ActivitySubTypeId { get; set; }
     public virtual ActivitySubType ActivitySubType { get; set; }
}

Then from the model ActivityRecord, I have a controller to save to the database. I am using scaffold controller to generate automatically. On the Create view, I create a cascading dropdown code. When ActivityType selected, the dropdown ActivitySubType will generate the items based on the data on the database. It saves to the database (success).

The case is, when I tried to Edit the record. How can I bind the ActivityTypeId on the ActivityType dropdown based on the value of ActivitySubTypeId. I already get it with the following code:

public async Task<IActionResult> Edit(int? id)
{
     var getActivityTypeId = (from a in _db.ActivityRecords
          join b in _db.ActivitySubTypes ON a.ActivitySubTypeId equals b.ActivitySubTypeId
          join c in _db.ActivityTypes ON b.ActivityTypeId equals c.ActivityTypeId
          where a.ActivitySubType == id
          select new {
               b.ActivityTypeId
          }
     ).FirstOrDefault();

     var activityTypeList = (from a in _db.ActivityTypes
                                select new SelectListItem()
                                {
                                    Text = a.ActivityTypeName, Value = a.ActivityTypeId.ToString()
                                }
           ).ToList();

      activityTypeList.Insert(0, new SelectListItem()
      {
          Text = "Select...",
          Value = ""
      });
      ViewBag.ActivityTypeDropList = activityTypeList;
}

On the dropdown:

<select id="ActivityTypeId" class="form-control form-control-sm" asp-items="@(new SelectList(ViewBag.ActivityTypeDropList,"Value", "Text"))">
</select>

How can I put the result of getActivityTypeId into the dropdown of ActivityType as a selectedValue ? Or maybe is there another method to achieve this?
Thank you.

CodePudding user response:

Well, after trial and error. I found the answers:

On the view:

<select class="form-control form-control-sm" asp-items="ViewBag.ActivityTypeId"></select>

On the Controller:

var selectedValue = (from a in _db.ActivityRecords
                             join b in _db.ActivitySubTypes on a.ActivitySubTypeId equals b.ActivitySubTypeId
                             join c in _db.ActivityTypes on b.ActivityTypeId equals c.ActivityTypeId
                             where a.ActivityRecordId == id
                             select new
                             {
                                 c.ActivityTypeId
                             }
            ).FirstOrDefault();

ViewData["ActivityTypeId"] = new SelectList(_db.ActivityTypes, "ActivityTypeId", "ActivityTypeName", selectedValue.ActivityTypeId.ToString());

Thank you for your attention.
Cheers,

  • Related