Get String (.net core for Jquery) value

Asked

Viewed 160 times

1

I am developing a site with . net core and I am using Jquery. On the page Index.cshtml.cs I have a function that has the validations and I pass the messages (if it is ok, if there was an error), through a string. Help me, because I’m new to . Net and I have no idea how to do.

I need, in Jquery, to take the value of this string to return the message handled.

I thank you all.

Code:

public async Task OnPostAsync(parametros)
{
    ....
    await AtivacaoVerificarAsync2(Parametros);

   //*Pegar o valor da mensagem aqui*

    return;
}

public async Task AtivacaoVerificarAsync2(Parametros)
{
     Mensagem = "deu certo";
     return;
}

Jquery:

$(document).ready(function () {
    $("#numeroSerie").focus();

    $("#form-login").on("submit", function () {
        $.ajax({
            url: "https://localhost:44390/",
            data: {},
            complete: function (xhr, statusText) {
                if (xhr.status == 200) {
                    alert(myValue);//*ALERTAR A MENSAGEM AQUI*
                } else {
                    alert(myValue);//*ALERTAR A MENSAGEM AQUI*
                }
            }
        });
    });
});

I can’t use Scriptmanager.Registerstartupscript, because of the Scriptmanager error, saying that it doesn’t exist in the current context, and I can’t use System.Web.UI either.

  • To take a value of one input with jQuery just do $("id_ou_classe_ou_input[name='seuInput']"). val()

  • but it is not the value of an input, which I need... I need the value of a string that is within a method

  • No code available, no way to know.

  • I added a code base so you have a notion there, if you can help me I appreciate

  • When you access https:/localhost:44390/, what does it return?

  • it returns to my index page, normal

  • 2

    Your project is ASP.NET Core Razor Pages?

  • @Rafael yes, he is

Show 3 more comments

1 answer

1


Hello, you can change the action to return what you need, probably Antiforerytoken() will also be a problem since it is turned on as default and your calls will return error 400. If you don’t use Razor Pages features it might be interesting to use ASP.NET Core MVC, it would be nice to also update and improve your ajax calls.

Ex.:

Script: (Index.cshtml)

    @page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

<form id="form-login">
    @Html.AntiForgeryToken()
    <input id="NumeroSerie" type="text" />
    <button type="submit">Submit</button>
</form>

@section scripts{
    <script>

        $(document).ready(function () {
            $("#NumeroSerie").focus();

            $("#form-login").on("submit", function (evt) {
                evt.preventDefault();
                $.ajax({
                    url: "/",
                    method: "POST",
                    data: { NumeroSerie: $("#NumeroSerie").val() },
                    headers: {
                        RequestVerificationToken:
                            $('input:hidden[name="__RequestVerificationToken"]').val()
                    }

                })
                    .done(function (msg) {
                        alert(msg);
                    })
                    .fail(function (msg) {
                        alert("Erro " + JSON.stringify( msg));
                    });
            });
        });

    </script>

}

And in the model:

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebApplication4.Pages
{
    public class IndexModel : PageModel
    {
        [BindProperty]
        public string NumeroSerie { get; set; }

        public void OnGet()
        {

        }


        public Task<JsonResult> OnPostAsync() {
            var result = ValidaAlgo();
            return Task.Run(()=> new JsonResult(result));
        }

        private string ValidaAlgo()
        {
            return NumeroSerie;
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.