2

I'm hoping this is going to be a simple issue but I've tried lots of things and still get the same results. I'm porting a small Umbraco web site from .Net Framework 4.8 to .Net 6.0. The model binding works with Framework MVC because it's a bit more forgiving than .Net 6.0, I believe. The issue is that the string properties bind okay but integers do not bind and end up with the default zero values.

This is the jQuery code which sends the data:

function sendComment(commentObj) {
$.ajax({
    type: "POST",
    url: "/umbraco/surface/BlogSurface/HandleSubmit/",
    dataType: "json",
    contentType: "application/json",        
    data: JSON.stringify(commentObj),
    success: function (result) {

and this shows the data about to be sent (note the blogPostId property):

Client side data

When the data is received and bound to the model at the server side, this is the result:

Server side data As you can see, the BlogPostId is now zero. The server endpoint being called is

[HttpPost]
//[ValidateUmbracoFormRouteString]
public async Task<IActionResult> HandleSubmit(CommentPostModel model)
{
    if (!ModelState.IsValid)
    {
        return RedirectToCurrentUmbracoPage();
    }

The model is this:

public class CommentPostModel
{
    [Required]
    public string AuthorName { get; set; }

    [Required]
    [EmailAddress]
    public string AuthorEmail { get; set; }

    [Required]
    [MaxLength(500)]
    public string Message { get; set; }

    public string AuthorUrl { get; set; }

    
    public int BlogPostId { get; set; }
    public int ParentId { get; set; }
    public int Level { get; set; }
}

I've tried adding the [BindRequired] attribute to the BlogPostId property in the model but the result is the same except that it says the model is not valid. I've tried adding the [FromBody] attribute to the server side endpoint but that then results in a 415 error because it insists on sending the data as multipart/form-data , even though the content-type is set to application/json.

Really at the point of just going with good 'ol .Net Framework

Update: To answer some of the questions posed: I tried adding the

[JsonPropertyName("BlogPostId")]

from System.Text.Json.Serialization to the BlogPostId property but this did not change the outcome.

Yes, the two screenshots above show what is being sent at the browser (using the Dev Tools) and the other is what was received at the server from the ajax call, obviously after model binding had taken place. Repeating this test and setting the other integer values at the browser (i.e. parentId and level) to 123 and 2 respectively, the result was all integers were zero on the server after model binding. Adding the [FromBody] to the method does trigger the form submit, although I'd like to prevent the form submit and just do it via ajax (I guess ensuring true is returned on the submit click?) Below shows the part of the view code for the form submit button:

<div class="col-md-6">
   <button class="btn btn-outline-primary btn-round mt-4" onclick="submitComment('CommentForm')">Post comment</button>
</div>
John J Smith
  • 11,435
  • 9
  • 53
  • 72
  • This is really unusual. Is the screenshot you posted showing what your browser dev tools Network tab shows actually getting sent? Can you inspect the body of the string received by the server? – StriplingWarrior Jun 09 '22 at 20:23
  • Could you pls share your view code? I am guessing you trigger form submit instead of ajax, which will cause 415 when you add the `[FromBody]`. – Rena Jun 10 '22 at 03:26
  • System.Text.Json or Newtonsoft? https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to?pivots=dotnet-6-0 – Jeremy Lakeman Jun 10 '22 at 04:08
  • Could you please also test with parentid and level? So we are sure its the because they are integers – Julian Jun 10 '22 at 10:44

1 Answers1

0

Try [JsonProperty] attribute on you public int BlogPostId { get; set; }

Example: https://www.newtonsoft.com/json/help/html/jsonpropertyname.htm

IKomarovLeonid
  • 352
  • 1
  • 9