0

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:

enter image description here

Model state on processing POST request:

enter image description here

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; }
}
1
  • 1
    Unless you need to bind some value to that property, you can try excluding property [Bind(Exclude="MemberType")] , or specify custom name using BindProperty attribute. Commented Feb 17, 2020 at 8:41

1 Answer 1

0

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:

enter image description here

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

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.