razor-pages

How do return Page with a model in ASP.Net Core Razor Page

拥有回忆 提交于 2019-12-25 18:49:14
问题 How to I Redirect to a Page and Passing its model? Just like what we have in MVC return View(model: MyModel); What i have tried: return RedirectToPage("/Notify", new { Model = notifierVM }); Note: the Page I want to return has no PageModel behind 回答1: MVC has built in dictionary object TempData . You can serialize your model, put JSON string into TempData and then on the redirected action you can get and deserialize JSON string into object. public ActionResult Create(Booking item) { TempData[

I cannot retrieve connection string in DbContext class in .NET Core 2.2 Razor Pages

好久不见. 提交于 2019-12-25 17:20:36
问题 In Startup.cs Configure Services this works: var connection = Configuration["ConnectionStrings:DefaultConnection"]; services.AddDbContext<MyDbContext>( options => { options.UseSqlServer(connection); }); In my MyDbContext.cs class this doesn't work: using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using OESAC.Models; namespace OESAC.Models { public class MyDbContext : DbContext { public

An unhandled exception occurred while processing the request for _Partial Page

杀马特。学长 韩版系。学妹 提交于 2019-12-25 02:15:35
问题 Top Page level View is named "Create.cshtml" On that page I need to place multiple _partialViews.cshtml which I have made using razor pages: <div> <row> <div class="col-md-6"> @await Html.PartialAsync("~/Views/Shared/_PartialDelegates") </div> <div class="col-md-6"> @await Html.PartialAsync("~/Views/Shared/_PartialApps") </div> </row> </div> Now the problem is I am trying to call _PartialDelegates.cshtml w/ _PartialDelegates.cs _PartialDelegates.cshtml @page @model IMP.ECR.WEB.Views.Shared.

ASP.NET Core: DbSet to executing raw stored procedure

非 Y 不嫁゛ 提交于 2019-12-25 00:18:00
问题 I am using ASP.NET Core 2.0 and razor pages. The SQL table does not have a primary key and I will not be able to make changes to the SQL table. I am trying to use a stored procedure to retrieve the data from the table and show the resultant data in UI. Since there is no primary key available, I am getting error as - Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys. I would like to move the code from DBSet to Raw sql as defined in https://www

How to add authorization to RazorPages?

回眸只為那壹抹淺笑 提交于 2019-12-24 23:13:55
问题 I have looked at the current official Microsoft Docs and was unable to find anything properly covering how to handle authorization for RazorPages. I figured out that you can add the AuthorizeAttribute to the PageModel like so: // using Microsoft.AspNetCore.Authorization [Authorize] public class IndexModel : PageModel { ... } I don't want to repeat this for every page. Is there a better way? 回答1: You can configure authorization under the ConfigureServices method. Here is an example : services

Cannot get json data with ajax in Razor Pages [duplicate]

邮差的信 提交于 2019-12-24 22:53:42
问题 This question already has answers here : Example AJAX call back to an ASP.NET Core Razor Page (6 answers) Closed 2 years ago . i am trying to get some data from Razor Pages in ASP.NET Core 2.0. But the problem is that it does not returns any data. I also tried debugging it, it does not fire that method (OnGetProducts) at all. The model Index.cshtml.cs: private IProductRepository _productRepository; public IndexModel(IProductRepository repository) { _productRepository = repository; } public

How to detect postback in Razor Page?

烈酒焚心 提交于 2019-12-24 20:13:11
问题 I'm using a single razor page (cshtml) that has a form and @functions{} section to capture an OnPost() . All of that works fine. In the HTML section, I need to know when a post back has occurred and display a message. I'm not sure where or how that is done in a Razor Page. I've tried IsPost but that isn't available. I created a string property in the class and set a value on it in OnPost but once I'm in the view, the property is null . I assigned a value into ViewData["mystring"] but it is

Prevent automatic passing of URL parameter in Razor Pages

五迷三道 提交于 2019-12-24 19:18:05
问题 I'm using the .NET Core 2 "Razor Pages" format for a web app. It allows you to define how to map the URL to variable names with a simple directive at the top: @page "{personId:int?}" The above, for example, would map the URL /Person/Index/123 to the IndexModel.OnGetAsync(int personId) function so that personId would have a value of 123. However, I've noticed that these values seem to be automatically added to any URL they could be, if a link exists on the page. If I add the following to the

Using dash/hyphen in Razor Page filename - does compile but VS shows errors

喜你入骨 提交于 2019-12-24 18:25:09
问题 There seems to be an issue with the Razor Pages (2.1.0-preview1-007211) and IntelliSense in Visual Studio 2017 (15.4.0 preview 2.0) with the filenames containing dashes. In order to reproduce: Create a new razor page e.g. razorpagetest.cshtml In the code add @{ ViewData["Title"] = "Test123"; } Save and close the file Rename razorpagetest.cshtml to razor-pagetest.cshtml Save and close the file Reopen the file and you will see errors on the errors list (although it builds OK) Has anyone

Razor Pages .NET Core 2.1 Integration Testing post authentication

三世轮回 提交于 2019-12-24 10:55:33
问题 I am looking for some guidance... I'm currently looking at trying to write some integration tests for a Razor Pages app in .net core 2.1, the pages I'm wanting to test are post authentication but I'm not sure about the best way of approaching it. The docs seem to suggest creating a CustomWebApplicationFactory, but apart from that I've got a little bit lost as how I can fake/mock an authenticated user/request, using basic cookie based authentication. I've seen that there is an open GitHub