Home > Software design >  Model doesn't bind in POST controller method .Net Core MVC
Model doesn't bind in POST controller method .Net Core MVC

Time:10-20

I have a model:

    public class CompanyFooterUpdateModel
    {
        public CompanyFooterUpdateModel(){}
        public Guid CompanyId { get; set; }
        public string FooterHtml { get; set; }
    }

Controller:

    public async Task<IActionResult> SaveFooter([FromBody] CompanyFooterUpdateModel model)
    {
       //.... do something in Company controller
    }

View, with axios post:

function SaveCompanyFooter() {
    axios.post('@Url.Action("SaveFooter", "Company")',
        {
            model: {
                FooterHtml: window.HtmlEditor.getData(),
                CompanyId: "@Model.CompanyId"
            }
        }
    ).then(response => {
        // .... do something
    });
}

Then, I tried everything, [FromBody], anything else, nothing... Please explain, what I did wrong?

UPD1: so I have tried something like this too:

axios.post('@Url.Action("SaveFooter", "Company")',
    {
        "FooterHtml": window.HtmlEditor.getData(),
        "CompanyId": "@Model.CompanyId"
    }
)

and

 public async Task<IActionResult> SaveFooter(Guid CompanyId, string FooterHtml)...

UPD2: Controller action screenshot

JS in View

CodePudding user response:

try to remove " from javascript

axios.post('@Url.Action("SaveFooter", "Company")',
    {
        FooterHtml: window.HtmlEditor.getData(),
        CompanyId: "@Model.CompanyId"
    }
)

or try to add json header


axios.post('@Url.Action("SaveFooter", "Company")',
    {
        FooterHtml: window.HtmlEditor.getData(),
        CompanyId: "@Model.CompanyId"
    }
),
 {
    'Content-Type': 'application/json'
  })
  .then((response) => {
.....

and use this action

public async Task<IActionResult> SaveFooter([FromBody] CompanyFooterUpdateModel model)
    {
       //.... do something in Company controller
    }
  • Related