开始修改使select兼容json和html两种方式

Former-commit-id: 8dc4e1562b84d7ba68848af46508440ca9bb7d2a
TangShanKaiPing
wanggang 5 years ago
parent d7f544da5c
commit c0ed737c44

@ -1,8 +1,8 @@
@model List<Guid> @model List<SelectListItem>
@if (Model != null && Model.Count > 0) @if (Model != null && Model.Count > 0)
{ {
foreach (var id in Model) foreach (var item in Model)
{ {
<a href="javascript:;" class="btn btn-default btn-xs">@ViewData.Get(id)</a> <a href="javascript:;" class="btn btn-default btn-xs">@item.Text</a>
} }
} }

@ -1,5 +1,5 @@
using Infrastructure.Application; using Infrastructure.Application;
using System; using Microsoft.AspNetCore.Mvc.Rendering;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
@ -18,6 +18,6 @@ namespace Application.Models
[DataType("MultiSelectList")] [DataType("MultiSelectList")]
[Display(Name = "角色")] [Display(Name = "角色")]
public List<Guid> Roles { get; set; } = new List<Guid>(); public List<SelectListItem> Roles { get; set; } = new List<SelectListItem>();
} }
} }

@ -6,7 +6,9 @@ using Infrastructure.Extensions;
using Infrastructure.Web.Mvc; using Infrastructure.Web.Mvc;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System;
using System.Linq; using System.Linq;
namespace IoT.Shared.Areas.Admin.Controllers namespace IoT.Shared.Areas.Admin.Controllers
@ -30,33 +32,29 @@ namespace IoT.Shared.Areas.Admin.Controllers
public override void ToDisplayModel(User entity, EditUserModel model) public override void ToDisplayModel(User entity, EditUserModel model)
{ {
model.Roles = entity.UserRoles.Select(o => o.RoleId).ToList(); model.Roles = entity.UserRoles.Select(o => new SelectListItem { Value = o.RoleId.ToString(), Text = o.Role.Name }).ToList();
entity.UserRoles.ForEach(o => ViewData.Add(o.RoleId, o.Role.Name)); //entity.UserRoles.ForEach(o => ViewData.Add(o.RoleId, o.Role.Name));
} }
public override void ToEditModel(User entity, EditUserModel model) public override void ToEditModel(User entity, EditUserModel model)
{ {
if (entity != null) this.ViewData.MultiSelectList(o => model.Roles, () => this._ajax.GetRoleMultiSelectList(model.Roles.Select(o => Guid.Parse(o.Value))));
{
model.Roles = entity.UserRoles.Select(o => o.RoleId).ToList();
}
this.ViewData.MultiSelectList(o => model.Roles, () => this._ajax.GetRoleMultiSelectList(model.Roles));
} }
public override void ToEntity(EditUserModel model, User entity) public override void ToEntity(EditUserModel model, User entity)
{ {
foreach (var id in entity.UserRoles.Select(o => o.RoleId).ToList()) foreach (var roleId in entity.UserRoles.Select(o => o.RoleId).ToList())
{ {
if (!model.Roles.Any(o => o == id)) if (!model.Roles.Any(o => o.Value == roleId.ToString()))
{ {
entity.UserRoles.RemoveAll(o => o.RoleId == id); entity.UserRoles.RemoveAll(o => o.RoleId == roleId);
} }
} }
foreach (var id in model.Roles) foreach (var role in model.Roles)
{ {
if (!entity.UserRoles.Any(o => o.RoleId == id)) if (!entity.UserRoles.Any(o => o.RoleId.ToString() == role.Value))
{ {
entity.UserRoles.Add(new UserRole { RoleId = id }); entity.UserRoles.Add(new UserRole { RoleId = Guid.Parse(role.Value) });
} }
} }
} }

Loading…
Cancel
Save