简体   繁体   中英

C# MVC disable binding Controller Method Name to Action property of the model

The problem is - my controller binds method name to the "Action" property of my model and of course it causes error in my model state because Method Name is String and my Action property is Object . I want the Action property to be null when it is not present in my Formdata.

Form data:

在此处输入图片说明

Model state on processing POST request:

在此处输入图片说明

My controller action:

[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);
        }
    }

My Model:

[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; }
}

After exploring the documentation i've noticed that the problem is caused by my route mapping that is using the "Action" parameter name in it, so it was conflicting with Action property of my model:

在此处输入图片说明

I've fixed it by adding a binding prefix to my Form, so no conflicting binding paramers longer exists:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM