Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#MVC禁用将控制器方法名称绑定到模型的Action属性_C#_Asp.net Mvc_Model Binding - Fatal编程技术网

C#MVC禁用将控制器方法名称绑定到模型的Action属性

C#MVC禁用将控制器方法名称绑定到模型的Action属性,c#,asp.net-mvc,model-binding,C#,Asp.net Mvc,Model Binding,问题是-我的控制器将方法名称绑定到我的模型的“Action”属性,当然,这会在我的模型状态中导致错误,因为方法名称是String,而我的Action属性是Object。我希望Action属性在Formdata中不存在时为null 表单数据: [HttpPost] public ActionResult RouteRoleActionsSave(TskRouteRoleAction model,List<TskAction.Actions> RoleActions = null

问题是-我的控制器将方法名称绑定到我的模型的“Action”属性,当然,这会在我的模型状态中导致错误,因为方法名称是String,而我的Action属性是Object。我希望Action属性在Formdata中不存在时为null

表单数据:

[HttpPost]
    public ActionResult RouteRoleActionsSave(TskRouteRoleAction model,List<TskAction.Actions> RoleActions = null)
    {
        using (var context = new SmartDbContext())
        {
            if (ModelState.IsValid)
            {
               return Json("ok");
            }
            return Json(ModelState);
        }
    }
[Table("TSK_ROUTE_ROLE_ACTIONS")]
public class TskRouteRoleAction
{
    [Key, Column("ROUTE_ID", Order = 0), ForeignKey("Route")]
    public decimal RouteId { get; set; }
    public TskRoute Route { get; set; }

    [Key, Column("ROLE_ID", TypeName = "numeric", Order = 1), ForeignKey("Role")]
    public TskRole.Roles? RoleId { get; set; }
    public TskRole Role { get; set; }

    [Key, Column("ACTION_ID", TypeName = "numeric", Order = 2), ForeignKey("Action")]
    public TskAction.Actions? ActionId { get; set; }
    public TskAction Action { get; set; }
}

处理POST请求时的模型状态:

[HttpPost]
    public ActionResult RouteRoleActionsSave(TskRouteRoleAction model,List<TskAction.Actions> RoleActions = null)
    {
        using (var context = new SmartDbContext())
        {
            if (ModelState.IsValid)
            {
               return Json("ok");
            }
            return Json(ModelState);
        }
    }
[Table("TSK_ROUTE_ROLE_ACTIONS")]
public class TskRouteRoleAction
{
    [Key, Column("ROUTE_ID", Order = 0), ForeignKey("Route")]
    public decimal RouteId { get; set; }
    public TskRoute Route { get; set; }

    [Key, Column("ROLE_ID", TypeName = "numeric", Order = 1), ForeignKey("Role")]
    public TskRole.Roles? RoleId { get; set; }
    public TskRole Role { get; set; }

    [Key, Column("ACTION_ID", TypeName = "numeric", Order = 2), ForeignKey("Action")]
    public TskAction.Actions? ActionId { get; set; }
    public TskAction Action { get; set; }
}

我的控制器操作:

[HttpPost]
    public ActionResult RouteRoleActionsSave(TskRouteRoleAction model,List<TskAction.Actions> RoleActions = null)
    {
        using (var context = new SmartDbContext())
        {
            if (ModelState.IsValid)
            {
               return Json("ok");
            }
            return Json(ModelState);
        }
    }
[Table("TSK_ROUTE_ROLE_ACTIONS")]
public class TskRouteRoleAction
{
    [Key, Column("ROUTE_ID", Order = 0), ForeignKey("Route")]
    public decimal RouteId { get; set; }
    public TskRoute Route { get; set; }

    [Key, Column("ROLE_ID", TypeName = "numeric", Order = 1), ForeignKey("Role")]
    public TskRole.Roles? RoleId { get; set; }
    public TskRole Role { get; set; }

    [Key, Column("ACTION_ID", TypeName = "numeric", Order = 2), ForeignKey("Action")]
    public TskAction.Actions? ActionId { get; set; }
    public TskAction Action { get; set; }
}

在浏览文档之后,我注意到问题是由路由映射引起的,该映射在其中使用了“Action”参数名称,因此它与模型的Action属性冲突:

我通过在表单中添加绑定前缀修复了此问题,因此不再存在冲突的绑定参数:


除非需要将某些值绑定到该属性,否则可以尝试排除属性[bind(Exclude=“MemberType”)],或使用BindProperty属性指定自定义名称。