I am currently working on a project but I have been stuck here for a day now. In summary the question I have depends only on the StartPage of my Project.
I have looked through countless guides even on StackOverflow but to no avail.
HTML
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Starter</title>
<style>
p {
margin-top: 30%;
margin-left: 20%;
margin-right: 20%;
font-family: Arial;
font-size: 25px;
text-align: center;
}
#Code {
border: 2px solid black;
}
</style>
</head>
<body>
<h1>249765876358312345655</h1>
@using (Html.BeginForm())
{
<p>
Enter a Machine Code:
<br />
<input id="Code"
name="Code"
pattern=""
size="30"
spellcheck="false"
title="Maschine Code"
value="">
<input type="hidden" value="26" name="projectId" />
</p>
<script>
let x = document.getElementById("Code");
x.addEventListener('input', function (event) {
x = document.getElementById("Code").value;
let vars = x;
let digits = vars.match(/^\d{13}(\d{6})\d{2}$/)[1];
let stringDigits = digits.toString();
if (stringDigits.length == 6 && vars.length == 21) {
window.location.href = '/home/Kontrolle';
document.getElementById("Code").innerHTML = "";
localStorage.setItem("Code_Kurz", stringDigits);
$.ajax({
url: "@Url.Action("StartPage")",
type: "GET",
data: JSON.stringify(stringDigits),
dataType: "string",
contentType:"StartPage/json",
success: function (data) { alert("Succ: " + data); },
error: function (xhr, status, error) {
var errorMessage = xhr.status + ': ' + xhr.statusText;
console.log("ERROR: " + errorMessage);},
});
}
});
</script>
}
</body>
</html>
To sum that up: the h1 Tag is a example of this Code witch gets filtered by ReGex -> end result: 123456
The number 123456 is saved in stringDigits. Now I want to pass stringDigits to the Controller.
Controller
namespace Qualitätskontrolle.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public IActionResult StartPage(string Code)
{
Debug.WriteLine(Code);
return View();
}
}
}
There in the StartPage Controller should the string then appear.
The result should be in the Output window: 123456 but I only get the error message from javascript.