-1

How to send js variables to mvc controller

I am trying to work out the code provided as answer to the above question by - ramiramilu.

However It seems I am still getting 0 as output in my controller.

The model and view look the same as the answer suggests. However, as I am using dot net core with MVC my code in controller side looks like -

Controller code -

[Route("api/[controller]")]
[ApiController]
public class PieController : Controller
{
    [HttpGet]
    public IActionResult Index()
    {
        return View();
    }

    public ViewResult GetChartDetails(PieConcs piecord)
    {
        return null;
    }
}

Javascript code -

function sendJson(){

    var model = new Object();
    model.x1 = 120;
    model.y1 = 240;
    model.x2 = 360;
        model.y2 = 480;
        alert('here');


    jQuery.ajax({
        type: "POST",
        url: "@Url.Action("GetChartDetails")",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ piecord: model }),
        success: function (data) {
            alert(data);
        },
        failure: function (errMsg) {
            alert(errMsg);
        }
    });
}

Model class -

public class PieConcs
{
    public int x1 { get; set; }
    public int x2 { get; set; }
    public int y1 { get; set; }
    public int y2 { get; set; }
}

The code for view is written in the Index.cshtml file. I am new to MVC, so please let me know if anything more in detail is required to better the question.

7
  • What exactly is the problem? It's not clear what you're asking. Where are you "getting 0 as output"? Commented Jan 4, 2019 at 14:30
  • Can you share your JS? Commented Jan 4, 2019 at 14:32
  • As it stands you're not outputting any number from your controllers. You're outputting a View and a null value. Commented Jan 4, 2019 at 14:33
  • @David - I am fetching the values in controller - same as the answer (for the question that I am following) Commented Jan 4, 2019 at 14:40
  • @AbhishekD: What "values"? Are you asking why your GetChartDetails method isn't returning a value? It's returning null. What are you expecting this code to do and why? Please clarify details about the problem you're facing. Stack Overflow is not a scavenger hunt, while your question can certainly reference other questions, it should not be dependent upon other questions for clarity and content. Commented Jan 4, 2019 at 14:43

1 Answer 1

0

In your javascript do something like:

let dataToSend = {
        objectProp1: ...,
        objectProp2: ...,
        ...
    }

    fetch('Pie/GetCharDetails', {
        method: 'POST',
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(dataToSend)
    }).then(response => response.json());

Keep in mind that object props must have exaclty the same name as your PieConcs props.

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.