преобразование asp.net mvc в страницу бритвы - PullRequest
0 голосов
/ 08 июня 2018

У меня есть веб-приложение asp.net, которое вызывает действие контроллера со следующим кодом:

 $(function () {
        $("#barcode").on("change", function (e) {
            // get the current value
            var barcode = $('#barcode').val();

            // if there's no text, ignore the event
            if (!barcode) {
                return;
            }
           // clear the textbox
            $("#barcode").val("");

           // var holdit = $('#textArea1').val();
            $('#textArea1').val($('#textArea1').val() +' '+ barcode);
             // post the data using AJAX
            $.post('@Url.Action("scanned", "receiveScan")?barcode=' + barcode);
        });
    })

Контроллер:

 [Produces("application/json")]
[Route("api/receiveScan")]
public class receiveScanController : Controller
{
    private static readonly HttpClient client = new HttpClient();

    public ActionResult scanned(string barcode)
    {

    var test = barcode;
        receiveScanModel newScan = new receiveScanModel();
        newScan.Barcode = barcode;
        newScan.companyNo = 1;

        string jsonScan = JsonConvert.SerializeObject(newScan, Formatting.Indented);

        var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://notarealservicehere.azurewebsites.net//api/receivescan");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            streamWriter.Write(jsonScan);
        }
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
        }


        return Ok();

Пытаюсь перейти на мою первую страницу Razor,все работает за исключением (очевидно) части $ .post ...

Куда это приведет?

Это базовое приложение asp.net со страницами бритвы

1 Ответ

0 голосов
/ 08 июня 2018

Используйте обработчик, например

в вашем файле cs

 public IActionResult OnPostBarcode(string barcode)

в вашем js var uri = "myPage /? Handler = Barcode"

$.post( uri ,{barcode:barcode}, function( data ) {
 console.log(data)
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...