0

Question: Can we bind without the use of a model and just purely based on the parameter names of the IActionResult method. From the below I am posting a single string to the end point, however it is wrapped in a object with a property of reviewNotes. I have a work around at the moment, which is explained below.

I have setup the end point llike so.

    [HttpPost("updateassetnotes/{id}")]
    public IActionResult UpdateAssetNotes(int id, [FromBody]string reviewNotes)
    {
        _dataMapper.UpdateAssetNotes(id, reviewNotes);
        return ApiSuccess("Updated Review Notes");
    }

And the client side is posting like this.

    var url = "/api/cataloguing/updateassetnotes/" + id;
    var data = { reviewNotes: self.assetReviewNotes() };

    $.ajax({
        type: 'POST',
        url: url,
        data: JSON.stringify(data),
        contentType: 'application/json'
    }).done(function (resp) {

    }).fail(function (resp) {

    }).always(function () {

    });

However if I change the data to:

    var data =  self.assetReviewNotes();

Then the review notes string is actually populated. As mentioned this is the work around im using at the moment. As previously mentioned I could create a simple model like the below and use this in the end point to bind the string. Im just wondering if there is a way to bind directly to the primitive types.

public class SimpleModel {
    public string ReivewNotes {get;set;}
}
4
  • 4 months ago i created demo with ASP.NET Core and post data with ajax. Mby it can help you github.com/CShepartd/ASP.Net_Core_-_Ajax_Example Commented May 24, 2017 at 13:36
  • @J.Doe I have just tried to implement a similar solution to yours, however it didnt work. Im guessing its because im trying to combine route parameters and post data. Commented May 24, 2017 at 13:48
  • Why do you want JSON.stringify data object with no name? With stringify request looking {"reviewNotes":"sss"} and without reviewNotes=sss Commented May 24, 2017 at 13:55
  • @J.Doe the ajax code is actually part of a wider library, so its been made generic to cope with an array of models, which is why the stringify is there. I have tried it several ways without the stringify and just the data object. Even with multiple variables in the end point. The two that work is the single string as the data object. Or creating a simple model for the end point as menioned in the question. Im mainly just curious if the parameters can be bound based on their names. Commented May 24, 2017 at 14:01

1 Answer 1

2

Obviously, as you already noted in your question

var data = { reviewNotes: "foo" };

is different from

var data = "foo";

as the latter is a string and the former is an object with a string property.


The most straightforward solution to your code comes to this line

data: JSON.stringify(data),

You could solve this by "unwrapping" the string out of the object by doing something like

data: JSON.stringify(data.reviewNotes),

The alternative is to avoid using JSON.stringify and just deal with the mapping the raw data instead. However, if you go this route, you may need to watch out for the mapping, and the [FromBody] decorator may need some tweaking, along with the content-type.

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.