Use of await in Razor views

前端 未结 8 909
面向向阳花
面向向阳花 2020-11-28 12:48

Is it possible to await on tasks in Razor .cshtml views?

By default it complains that it can only be used in methods marked with async so I

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 12:57

    I landed on this question because I am a newbie to Razor and I wanted to display a simple "loading..." screen while my Controller Code was calculating data.

    So I found this link: https://www.codeproject.com/Articles/424745/MVC-Razor-In-Progress-Icon which was helpful, but because I was a total novice at Razor, I was unable to make this work.

    What finally worked for me was the following.

    1) Add the "loading" div as suggested in the code project to my .cshtml file:

    
    

    2) Modify my Razor form from

    
    

    to

    
    

    3) Create the JavascriptFunction() in my .cshtml page:

    
    

    If I understand all of the above correctly, what this does is execute the function JavascriptFunction when I press the Go button.

    The JavascriptFunction does 2 things: 1) Change the view of the page by showing the previously hidden (display:none) divLoading div. 2) Submit all the forms on this page (I only have one, so it submits the form the same as if I had they type submit on the button)

    After the Controller launched by the form submit is done, it loads a new view on a new page, and the initial page (and the "loading" div) is gone. Mission accomplished.

提交回复
热议问题