0

I have the following javascript:

<script>
function uploadPhotoDoc() {
    var SmallPhotoDoc = {};
    SmallPhotoDoc.PK_PhotoDoc = $("#PK_PhotoDoc").val();
    SmallPhotoDoc.FK_Site = $("#FK_Site").val();
    SmallPhotoDoc.SeriesID = $("#SeriesID").val();
    SmallPhotoDoc.ContentID = $("#ContentID").val();
    SmallPhotoDoc.Notes = $("#Notes").val();
    SmallPhotoDoc.Date = $("#Date").val();
     
    data = "{SmallPhotoDoc: " + JSON.stringify(SmallPhotoDoc) + "} ";

    alert(data);
    $.ajax({
        type: "POST",
        url: "/Containers/UploadPhotoDoc/@ViewContext.RouteData.Values["org"]",
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert(response);
        },
        failure: function (response) {
            alert(response.responseText);
        },
        error: function (response) {
            alert(response.responseText);
        }
    });
     
}
</script>

And I have following class in C#:

public class SmallPhotoDoc
{ 
    public Guid PK_PhotoDoc { get; set; }
    public Guid? FK_Site { get; set; }
    public string SeriesID { get; set; }
    public string ContentID { get; set; }
    public string Notes { get; set; }
    public DateTime? Date { get; set; }
}

And I have the following signature to be sent to:

public string UploadPhotoDoc(string id, SmallPhotoDoc model)

I have verified the data is correct with the alert popup. But when I go to the UploadPhotoDoc menu, the model is always null. Does anyone see my error in this? Note, I have tried to use [FromBody] with the model parameter and that didn't do anything. It show no form has been posted on the Request object.

2
  • Is this UploadPhotoDoc an action method on an ASP.NET MVC controller? Have you added a [HttpPost] annotation to it? Commented Feb 17, 2021 at 20:02
  • @marc_s It is a marked [HttpPost] and is part of a controller. Commented Feb 17, 2021 at 20:23

2 Answers 2

4

In JS:

  function uploadPhotoDoc(id) {

    var SmallPhotoDoc = {};
    SmallPhotoDoc.PK_PhotoDoc = $("#PK_PhotoDoc").val();
    SmallPhotoDoc.FK_Site = $("#FK_Site").val();
    SmallPhotoDoc.SeriesID = $("#SeriesID").val();
    SmallPhotoDoc.ContentID = $("#ContentID").val();
    SmallPhotoDoc.Notes = $("#Notes").val();
    SmallPhotoDoc.Date = $("#Date").val();

    var model = {
        "id": @ViewContext.RouteData.Values["org"],
        "model": SmallPhotoDoc
    }

    alert(model);

    $.ajax({
        type: "POST",
        url: "/Containers/UploadPhotoDoc",
        data: model,
        datatype: "json",
        cache: false,
        success: function (response) {
            alert(response);
        },
        failure: function (response) {
            alert(response.responseText);
        },
        error: function (response) {
            alert(response.responseText);
        }
    });

}

In controller's action method:

public string UploadPhotoDoc(string id, SmallPhotoDoc model)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this answer. Since I was getting the id from the route, I didn't think to add this.
0

After noting that the direct post worked, I changed the upload to the following and it now works:

$.ajax({
        type: "POST",
        url: "/Containers/UploadPhotoDoc/@ViewContext.RouteData.Values["org"]",
        data: $("#photodocform").serialize(),
       
        dataType: "json",
        success: function (response) {
            alert(response);
        },
        failure: function (response) {
            alert(response.responseText);
        },
        error: function (response) {
            alert(response.responseText);
        }
    });

I would still love to see the reason it didn't work from anyone who knows exactly what I did incorrectly.

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.