Skip to content

Commit

Permalink
修改EntityModelBinder,先解析json来填充对象
Browse files Browse the repository at this point in the history
  • Loading branch information
codekgithub committed Nov 6, 2024
1 parent fe4ba90 commit 04364f1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion NewLife.CubeNC/Areas/Admin/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace NewLife.Cube.Areas.Admin.Controllers;
[Description("系统基于角色授权,每个角色对不同的功能模块具备添删改查以及自定义权限等多种权限设定。")]
[AdminArea]
[Menu(100, true, Icon = "fa-user", HelpUrl = "https://newlifex.com/cube/cube_security")]
public class UserController : EntityController<User>
public class UserController : EntityController<User, UserModel>
{
/// <summary>用于防爆破登录。即使内存缓存,也有一定用处,最糟糕就是每分钟重试次数等于集群节点数的倍数</summary>
private readonly ICache _cache;
Expand Down
8 changes: 4 additions & 4 deletions NewLife.CubeNC/Common/EntityModelBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ protected override Object CreateModel(ModelBindingContext bindingContext)
// 如果提交数据里面刚好有名为model的字段,这是Add/Edit接口的入参,则需要清空modelName,否则无法绑定
if (bindingContext.ModelName == "model") bindingContext.ModelName = String.Empty;
}
if (!modelType.As<IEntity>()) return base.CreateModel(bindingContext);

var fact = EntityFactory.CreateFactory(modelType);
if (fact == null) return base.CreateModel(bindingContext);

// 尝试从body读取json格式的参数
var ctx = bindingContext.HttpContext;
Expand All @@ -50,6 +46,10 @@ protected override Object CreateModel(ModelBindingContext bindingContext)
// 强行绑定会出错记录在ModelState,在api中返回400错误,mvc不会
bindingContext.ValueProvider = cubeBodyValueProvider;
}
if (!modelType.As<IEntity>()) return base.CreateModel(bindingContext);

var fact = EntityFactory.CreateFactory(modelType);
if (fact == null) return base.CreateModel(bindingContext);

var pks = fact.Table.PrimaryKeys;
var uk = fact.Unique;
Expand Down

0 comments on commit 04364f1

Please sign in to comment.