Pagemodel.Page() is a method, which is not Valid in the Given context

Asked

Viewed 83 times

0

I’m needing to use a script from an Alert() in my project . NET core Razor, and when I add the line:

Page.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Hello World')", true);

Gives the following error:

'Pagemodel.Page()' is a method, which is not Valid in the Given context

Help me please, I don’t know how to fix it... I tried to remove the Page. and it goes like this:

The name 'Scriptmanager' does not exist in the Current context

And I can’t use the using System.Web.UI;

Method:

public async Task OnPostAsync(PARAMETROS){
      CancellationTokenSource backGroundTask = new CancellationTokenSource();
      MobAtivacaoAcesso MobAtivacaoAcesso = new MobAtivacaoAcesso("X", "Z");

      await MobAtivacaoAcesso.AtivacaoVerificarAsync(PARAMETROS); 

      if (!String.IsNullOrEmpty(MobAtivacaoAcesso.MensagemTemporaria)){
          Response.Headers.Add("mensagem", MobAtivacaoAcesso.MensagemTemporaria);
      }

      return;
}
  • After all, what kind of project are you with? MVC, Webforms, Razor Pages? which one?

  • Not to be rude, but it’s in question... . NET core Razor....

  • is Razor Pages and what in question is Web Forms then what you want to do because that code doesn’t work on Razor Pages?

  • What do you want to do with a alert? your code doesn’t work

  • i would like to make the Alert of a message that is being returned from a method

  • post the method?

  • Sure, @Virgilionovic, I’ll leave the code in the question

  • In case, if I could use the Script, it would be in place of this Response.Headers.Add("mensagem", MobAtivacaoAcesso.MensagemTemporaria);

Show 4 more comments

1 answer

0


Scriptmanager was traditionally using Webforms and Updatepanel to perform ajax and partial render requests, in ASP.NET Core you will no longer have that (I believe you will not even be migrated).

In this case, ASP.NET Core assumes in some of its templates the View Engine Razor (but its application could be a SPA, for example), which has a Markup that helps you in the development of the pages in general. Then it would be nice to know what you really need.

For your current scenario, folder add a script Section and run it in the page loading, or simply add some library to not mess with the DOM directly, such as jquery.

Anyway, you could just do that:

@{
        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>

@section Scripts {
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            alert('Olá!');
        }, false)
    </script>

}

You can send information to View using Viewbag, Models, Tempdata etc...

Controller:

  public IActionResult Index()
    {
        ViewBag.Mensagem = "Teste Mensagem";
        return View();
    }

View:

@section Scripts {
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            alert('@ViewBag.Mensagem'); 
        }, false)
    </script>

}

Then you need to study a little about the technology to get familiar, I suggest doing the tutorials for MVC and/ or Razor Pages:

https://docs.microsoft.com/pt-br/aspnet/core/tutorials/razor-pages/? view=aspnetcore-2.2

https://docs.microsoft.com/pt-br/aspnet/core/tutorials/first-mvc-app/? view=aspnetcore-2.2

Links about Razor:

https://www.w3schools.com/asp/razor_intro.asp

https://www.learnrazorpages.com/

  • So rafael, I’m starting to work with . NET for now, but I believe this would not work, because the content of the alert I need to show is in index.Cs, within a variable string

  • You need to put the whole scenario in the question! I improved the answer, it’s as much as I can help.

Browser other questions tagged

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