简体   繁体   中英

Model doesn't bind in POST controller method .Net Core MVC

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

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
    }

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