从 ViewBag DotNet Core 设置 SelectedValue
Set SelectedValue from ViewBag DotNet Core
我有以下型号:
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; }
}
然后从模型ActivityRecord
,我有一个控制器可以保存到数据库中。我正在使用脚手架控制器自动生成。在 Create
视图中,我创建了一个级联下拉代码。选择 ActivityType
时,下拉菜单 ActivitySubType
将根据数据库中的数据生成项目。它保存到数据库(成功)。
情况是,当我尝试编辑记录时。我如何根据 ActivitySubTypeId
的值在 ActivityType
下拉列表中绑定 ActivityTypeId
。我已经用下面的代码得到它:
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;
}
在下拉列表中:
<select id="ActivityTypeId" class="form-control form-control-sm" asp-items="@(new SelectList(ViewBag.ActivityTypeDropList,"Value", "Text"))">
</select>
如何将 getActivityTypeId
的结果作为 selectedValue
放入 ActivityType
的下拉列表中?或者是否有其他方法可以实现此目的?
谢谢。
好吧,经过反复试验。我找到了答案:
在视图中:
<select class="form-control form-control-sm" asp-items="ViewBag.ActivityTypeId"></select>
在控制器上:
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());
感谢您的关注。
干杯,
我有以下型号:
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; }
}
然后从模型ActivityRecord
,我有一个控制器可以保存到数据库中。我正在使用脚手架控制器自动生成。在 Create
视图中,我创建了一个级联下拉代码。选择 ActivityType
时,下拉菜单 ActivitySubType
将根据数据库中的数据生成项目。它保存到数据库(成功)。
情况是,当我尝试编辑记录时。我如何根据 ActivitySubTypeId
的值在 ActivityType
下拉列表中绑定 ActivityTypeId
。我已经用下面的代码得到它:
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;
}
在下拉列表中:
<select id="ActivityTypeId" class="form-control form-control-sm" asp-items="@(new SelectList(ViewBag.ActivityTypeDropList,"Value", "Text"))">
</select>
如何将 getActivityTypeId
的结果作为 selectedValue
放入 ActivityType
的下拉列表中?或者是否有其他方法可以实现此目的?
谢谢。
好吧,经过反复试验。我找到了答案:
在视图中:
<select class="form-control form-control-sm" asp-items="ViewBag.ActivityTypeId"></select>
在控制器上:
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());
感谢您的关注。
干杯,