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.
GetChartDetailsmethod isn't returning a value? It's returningnull. 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.