razor-pages

XHR POST: to API Controller works, to Razor Page fails

坚强是说给别人听的谎言 提交于 2020-01-16 09:01:16
问题 I have a standard ASP.NET Core Web application created from the Visual Studio template. I have a razor page which issues an XHR POST request with a payload upon a change event. If I make the url of the request an api controller in the application (i.e. under Controllers folder), the request succeeds. If I make the same request, same payload to a razor page method (under Pages folder), it fails with http 400. Both methods have the same signature. Can anyone describe why the behaviour is

How to define conditional model binding in ASP.NET Core Razor Pages?

淺唱寂寞╮ 提交于 2020-01-16 08:36:32
问题 I'm trying to make a simple login form in ASP.NET Core Razor Pages, which has an email address field, a password field, a login button and a forgot-password button. I want to utilize the inbuilt client-side and server-side validation on the text fields, such that the email field is required for both button submissions, but the password field is only required for login button submission. Decorating the model properties with a [Required] attribute makes them mandatory for all form post handlers

Calling Action in Razor Pages

一笑奈何 提交于 2020-01-11 03:16:06
问题 as a newbie to Razor Pages I have a question regarding calling methods from Razor Page. I have a method called subtractProduct defined in my domain model. In my Index Page code I define IActionResult sellProduct which calls subtractProduct on specified ProductId. But I have no clue how to call this method on my html page. I've tried a lot of combinations, but nothing seems to be working. Anyone knows how to deal with this? Any help is greatly appreciated! My domain model is: public class

ASP.NET Core 2.0 Razor pages vs Full MVC Core

怎甘沉沦 提交于 2020-01-10 06:52:50
问题 There has been a question at SO Why is Razor Pages the recommended approach to create a Web UI in Asp.net Core 2.0? where Steve Smith has kindly explained the benefits of using Razor Pages over full MVC from the perspective of having less files. I've been using Razor Pages for a while and noticed that despite an advantage of a Razor Page simplicity, it is a bit complicated when it comes to custom routing, structuring folders and complex view model (page model seem to be cluttered). So, the

How to get ASP.NET Identity authentication ticket expiry in Razor Page?

耗尽温柔 提交于 2020-01-06 07:21:43
问题 I'm using identityserver4 with ASP.NET Identity, with a cookie configured with SlidingExpiration = true and ExpireTimeSpan = 20 minutes. I would like to provide the user with a warning when they are about to timeout so am trying to access the ".expiry" value in the cookie. So far I have been able to read an expiry time using the Razor code below. However this is failing to read the correct expiry time when the user refreshes their ticket. According to the Microsoft docs SlidingExpiration

How to get ASP.NET Identity authentication ticket expiry in Razor Page?

天涯浪子 提交于 2020-01-06 07:21:09
问题 I'm using identityserver4 with ASP.NET Identity, with a cookie configured with SlidingExpiration = true and ExpireTimeSpan = 20 minutes. I would like to provide the user with a warning when they are about to timeout so am trying to access the ".expiry" value in the cookie. So far I have been able to read an expiry time using the Razor code below. However this is failing to read the correct expiry time when the user refreshes their ticket. According to the Microsoft docs SlidingExpiration

How to connect ASP.NET Core 2.2 Razor pages with MySql database?

自作多情 提交于 2020-01-06 04:31:12
问题 I'm developing an ASP.NET Core razor pages website since I'm a beginner. I'm trying to connect my web application with MySql database. Since I'm a beginner I don't really know how to connect my application with MySql database. I know how to connect with SQL Server, but I want to know about connecting to MySql. A complete working process with MySql and Asp.Net core 2.2 Razor pages would be much appreciated. 回答1: I suggest you take a look at Setup Entity Framework Core for MySQL in ASP.NET Core

Change Bound Property in OnPost in model is invalid

天大地大妈咪最大 提交于 2020-01-02 10:32:48
问题 I am using ASP.NET Core Razor Pages and I want to add a counter for a number of attempts it takes a user complete a task. I am using: [BindProperty] public int Attempts { get; set; } And inside the OnPost I am doing this: public IActionResult OnPost() { if(!IsCorrect()) { Attempts++; return Page(); } return RedirectToPage($"./Index") } I expected this to update the data on the client side, since without [BindProperty] & return Page() , data would be lost if the model was invalid. However,

Change Bound Property in OnPost in model is invalid

余生颓废 提交于 2020-01-02 10:31:40
问题 I am using ASP.NET Core Razor Pages and I want to add a counter for a number of attempts it takes a user complete a task. I am using: [BindProperty] public int Attempts { get; set; } And inside the OnPost I am doing this: public IActionResult OnPost() { if(!IsCorrect()) { Attempts++; return Page(); } return RedirectToPage($"./Index") } I expected this to update the data on the client side, since without [BindProperty] & return Page() , data would be lost if the model was invalid. However,

Cannot pass data with ajax to razor pages

梦想的初衷 提交于 2019-12-31 03:43:26
问题 I am trying to send some data to razor page method, but the problem is that in method it takes always as 0 . Heres the code: Razor Page: public IActionResult OnGetProducts(int page) { var products = _productRepository.GetProducts(); decimal pagesInDecimal = (products.Count() / 18); var totalPages = pagesInDecimal % 1 == 0 ? pagesInDecimal : pagesInDecimal + 1; products = products.Skip((page - 1) * 20).Take(20); return new JsonResult(new { totalPages = totalPages, products = products }); }