Thank you so much New York for your suggestions. Actually, HttpPost attribute was yet there, I just forgot to include it in this Post. To implement your suggestion, I created a new controller (Photo) with an ActionResult method (TakePhoto) and added the ajax code to the view (PublicInfo.cshtml):
@model Nop.Plugin.Widgets.Expressions.Models.PublicInfoModel
@{
Layout = "";
//Some Html here...
<div class="col-sm-6">
<form id="AjaxForm">
<input type="hidden" id="productId" value[email protected] />
<input type="hidden" id="expressionName" value[email protected] />
<input type="hidden" id="role" value[email protected] />
<input type="hidden" id="counter" value[email protected] />
<input type="hidden" id="input_elem" name="image" />
<button class="btn btn-success" id="submitbutton" type="submit" value="Submit">Save photo</button>
</form>
</div>
<script language="javaScript">
(function () {
//=====================================
$(document).ready(function () {
$("#AjaxForm").submit(function (e) {
e.preventDefault();
// Deshabilitamos el botón de Submit
$("#SubmitBtn").prop("disabled", true);
var data = canvas.toDataURL('image/png');
$.ajax({
url: "@Url.Action("TakePhoto", "Photo")", // Url
data: {
// Datos / Parámetros
ProductId: $("#productId").val(),
ExpressionName: $("#expressionName").val(),
Role: $("#role").val(),
Counter: $("#counter").val(),
Photo: $("#input_elem").val(data)
},
type: "post" // Verbo HTTP
})
</script>
And changed RouteProvider:
namespace Nop.Plugin.Widgets.Expressions
{
public partial class RouteProvider : IRouteProvider
{
public void RegisterRoutes(IEndpointRouteBuilder endPointRouteBuilder)
{
endPointRouteBuilder.MapControllerRoute("Plugin.Widgets.Expressions.WidgetsExpressions.Configure", "Plugins/WidgetsExpressions/Configure",
new { controller = "WidgetsExpressions", action = "Configure" });
endPointRouteBuilder.MapControllerRoute("Plugin.Widgets.Expressions.BookExpressions.AddExpressiontoBook", "Plugins/BookExpressions/AddExpressiontoBook",
new { controller = "BookExpressions", action = "AddExpressiontoBook" });
//This is my public endPoint
endPointRouteBuilder.MapControllerRoute("Plugin.Widgets.Expressions.Photo.TakePhoto", "Plugins/Photo/TakePhoto",
new { controller = "Photo", action = "TakePhoto" });
}
public int Priority => -1;
}
}
However, it is not working. Url route cannot be found. As controllers and the view component are within a dll file, I guess a reference has to be added to the assembly controller with
Microsoft.AspNetCore.Mvc.ApplicationParts, as mentioned
here. But the problem is that I don't know how to do that. Can anyone please explain how to do this in NopCommerce 3.1 ?
Also, I am not sure how the Url Route pattern goes in RouteProvider. I have tried with: "Photo/TakePhoto" "Plugins/Photo/TakePhoto", "Plugins/Widgets.Expressions/Photo/TakePhoto",with no luck. An explanation will be appreciated.
Thanks in advance.
Dumar