0

I am having trouble with the put method of Web API. I am working with Kendo UI Jquery inside ASP.NET MVC and it has to make a PUT through API.

Please guide me what am I doing wrong, also at the end of this is the error code.

Here it is what I have tried so far:

API controller:

[HttpPut] //It never reaches here
[Route("api/Update")]
public async Task<IActionResult> Update(Guid id, UpdateProductCommand command)
{
    if (id != command.Id)
    {
        return BadRequest();
    }

    return Ok(await _mediator.Send(command));
}

ASP.NET MVC controller:

public ActionResult Update(Guid id, [FromBody]Product product)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("https://localhost:44393");
            
        var json = JsonConvert.SerializeObject(product);

        var contentData = new StringContent(json, Encoding.UTF8, "application/json");

        var response = client.PutAsync("/api/Product", contentData);

        var result = response.Result;

        if (result.IsSuccessStatusCode)
        {
            return RedirectToAction("Index");
        }
    }

    return View(product);
}

View with Kendo UI:

<script>
    //----------TRANSPORT -------------------//
    var dataSource = new kendo.data.DataSource({
        batch: false,
        transport: {
            read: {
                url: "https://localhost:44393/api/Product",
                dataType: "json"
            },
            update: {
                url: "@Url.Action("Update", "Home")",
                dataType: "json",
            },
            create: {
                url: "@Url.Action("Create", "Home")",
                dataType: "json",
                contentType: "application/json",
                type: "POST"
            },
            destroy: {
                url: "@Url.Action("Delete", "Home")",
                dataType: "json",

            },
           },
        pageSize: 5,
        schema: {
            model: {
                id: "id",
                fields: {
                    id: { editable: false, nullable: true },
                    productName: { type: "string", editable: true },
                    prouctSKU: { type: "string", editable: true },
                    productType: { type: "string", editable: true },
                }
            }
        }
    });

Error:

jquery.min.js:4 GET https://localhost:44385/Home/Update?id=1e8332f1-ae69-4997-b851-08d9ae81e7de&productName=sd&prouctSKU=string&productType=string 415

0

2 Answers 2

1

In your MVC Controller you are using PutAsync in a non async method. Change your Action to an async method, change the ActionResult to async Task<ActionResult> and use the await keyword when calling the PutAsync.

Or if you do not want to change your method to an async method change the call client.PutAsync to client.Put

Sign up to request clarification or add additional context in comments.

1 Comment

If you want to read more information about Asynchronous programming I advise you to read the official documentation learn.microsoft.com/en-us/dotnet/csharp/programming-guide/… or there is a discussion on stack stackoverflow.com/questions/14455293/…
1

You need to tell your component to make a PUT request otherwise it will default to GET. According to the docs, you should specify the type of the request. For example:

update: {
    url: "@Url.Action("Update", "Home")",
    dataType: "json",
    type: "PUT" //<---- Add this
},

1 Comment

Thank you for the answer David, already tried it but doesn't work, I think my problem may be to my MVC Controller somehow.

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.