1 回答

TA貢獻1876條經驗 獲得超5個贊
像這樣試試
視圖模型:
public class ApplicationRolesViewModel
{
// Display Attribute will appear in the Html.LabelFor
[Display(Name = "User Role")]
public string RoleId { get; set; }
public IEnumerable<SelectListItem> Roles { get; set; }
}
控制器:
public ActionResult NewRole()
{
var roleData = new IEnumerable<SelectListItem>();
applicationRolesData.GetAllApplicationRoles().Foreach(x =>
roleData.Add( new SelectListItem
{
Value = x.RoleId.ToString(),
Text = x.ApplicationRoleName
});
);
ApplicationRolesViewModel ardlvm = new ApplicationRolesViewModel();
ardlvm.Roles = new SelectList(roleData , "Value", "Text")
return View("~/Views/Users/Modals/AddRole.cshtml", ardlvm);
}
查看:
@model ApplicationRolesViewModel
@Html.LabelFor(m => m.RoleId)
@Html.DropDownListFor(m => m.RoleId, Model.Roles)
并獲取當前選擇的下拉列表值Jquery:
$('#RoleId').val();
要獲取當前選定的文本:
$('#RoleId:selected').text();
- 1 回答
- 0 關注
- 283 瀏覽
添加回答
舉報