Making post to Web Api Controller.

Hello everybody,

today I want to share with you one small note, how to make post of some js object to controller.

So, let's say you have following class:

public class SomeClass : MongoEntityBase
        public ObjectId TenderId { getset; }
        public int RectificationNumber { getset; }
        public RectificationReason RectificationReason { getset; }
        public string RectificationText { getset; }

And some method in your web api controller:

public async Task<SomeClass> CreateRectification(SomeClass  someInstance)
{        await _dbContext.TenderRectifications.InsertOneAsync(someInstance);
     return someInstance;

And let's say you want to pass into controller instance of SomeClass. How to achieve this?

One of the ways could be following js code with help of jQuery and Html:

<script src=""></script>
<script type="text/javascript">
    function SendRectification() {
        var rectification = {};
        rectification.TenderId = '';
        rectification.RectificationNumber = '1';
        rectification.RectificationReason = '2';
        rectification.RectificationText = 'Date changed';
            url: '/TenderRectifications/create',
            async: false,
            type: "POST",
            data: JSON.stringify(rectification),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            error: function(jqXHR, textStatus, errorThrown) {
                alert(jqXHR + "-" + textStatus + "-" + errorThrown);
            success: function(data, textStatus, jqXHR) {
<input type="button" value="Create rectification" onclick="return SendRectification();"/>
<div id="Results"></div>
<div id="ResultMessage"></div>

No Comments

Add a Comment