How to create the code behind of Razor page - asp.net

When I create a new Razor page in Visual Studio 2017, the separated code behind file did not get created.
This is the screenshot of the problem highlighted.
We can see the Razor page called List.cshtml does not contain the code behind file. Contrast that with the default page about.cshtml, for example.

1 - Right click on "resturants", add class:
2 - in your case, call it List.cshtml.cs. This will create the code behind. It nows needs wiring up. Open the class that has been created.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
//Add the MVC usings
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
//TODO The namespace will be correct in your code
namespace app_name.Pages.resturants
{
//public class List
//TODO correct the model name and inherit PageModel
public List1Model : PageModel
{
public void OnGet()
{
}
}
}
3 - In List.cshtml:
#page
#model app_name.Pages.resturants.List1Model
#{
}
More information can be found here on modifying the code behind https://learn.microsoft.com/en-gb/aspnet/core/data/ef-rp/sort-filter-page?view=aspnetcore-5.0

Related

Acumatica How to customize Time Entries and Activities with Custom Selector Field

We want to customize the time entries and activities logic to have a custom selector field that pulls information from an attribute that is setup in the system. It has to be a required field if the TimeSpent and BilledTime does not match, the entry is marked as billable, and the project has a customer loaded against it. I have successfully managed to get it working on the Employee Time Activities screen, but I am trying to use that same custom field on the Activities popup dialog. I also get an error when adding an Activity directly to project, which states that the Time Entry requires a Reason Code but I don't have access to add it to the list to display on the Activities Panel/Dialog.
See screenshots below:
Ignore the Activity Reason Code, this is an additional custom field I added to test the customization.
I want to add my custom field in here:
Below is the extention class DAC.
using PX.Data;
using PX.Objects.CS;
using PX.Objects.PM;
using iPlanReasonCode.Helpers;
namespace PX.Objects.CR
{
public class PMTimeActivityExt : PXCacheExtension<PX.Objects.CR.PMTimeActivity>
{
#region UsrReasonCode
[PXDBString]
[PXUIField(DisplayName = "Reason Code")]
[PXDefault]
[PXSelector(typeof(Search<CSAttributeDetail.valueID, Where<CSAttributeDetail.attributeID.IsEqual<ReasonCodeAttributeID>>>),
typeof(CSAttributeDetail.valueID), typeof(CSAttributeDetail.description), SubstituteKey = typeof(CSAttributeDetail.valueID), DescriptionField = typeof(CSAttributeDetail.description))]
[PXUIRequired(typeof(Where<PMTimeActivity.isBillable.IsEqual<True>.And<PMTimeActivity.timeSpent.IsNotEqual<PMTimeActivity.timeBillable>>.And<Where<Selector<PMTimeActivity.projectID, PMProject.customerID>, IsNotNull>>>))]
public virtual string UsrReasonCode { get; set; }
public abstract class usrReasonCode : PX.Data.BQL.BqlString.Field<usrReasonCode> { }
#endregion
}
}
Below is the helper constant class to point it to the correct attribute code.
using PX.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace iPlanReasonCode.Helpers
{
public class ReasonCodeAttributeID : PX.Data.BQL.BqlString.Constant<ReasonCodeAttributeID>
{
public ReasonCodeAttributeID()
: base("CUSTREASON")
{ }
}
}
So this one is a bit tricky, for some reason Acumatica's "Add data fields" on the customization project browser does not recognize the "TimeActivity" view, which is the view in which your field is added to (on PMTimeActivity).
What you should do if you want to keep the field on this record is manually edit the ASPX, find the area of the page where you see fields referencing Datafield="TimeActivity.***" and manually add an XML Tag and reference the timeactivity data view the same way e.g. DataField="TimeActivity.UsrYourCustomField"
Textbox:
<px:PXTextEdit ID="chkYourField" runat="server" DataField="TimeActivity.UsrYourCustomField" />
Checkbox:
<px:PXCheckBox ID="chkYourField" runat="server" DataField="TimeActivity.UsrYourCustomField" />
After you manually add the tag, then you will be able to move it around using the UI
If you want to skip over all of this and just make it easy, move your custom field to the CRActivity DAC

Why the tabs are not in the bottom in Xamarin Forms?

Fellow Developers,
I'm developing an app with Xamarin.Forms and I am using the latest version 3.1, which as far as I know it supports Bottom Tabs in Android. This is my code:
XAML:
<?xml version="1.0" encoding="utf-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom"
android:TabbedPage.BarItemColor="#666666"
android:TabbedPage.BarSelectedItemColor="Black"
BarBackgroundColor="#2196F3"
BarTextColor="White"></TabbedPage>
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace BottomTabsExample
{
public partial class MainPage : TabbedPage
{
public MainPage()
{
var navigationPage = new NavigationPage(new History());
navigationPage.Icon = "ic_history.png";
navigationPage.Title = "History";
var navigationPage2 = new NavigationPage(new History());
navigationPage2.Icon = "ic_earth.png";
navigationPage2.Title = "Earth";
Children.Add(navigationPage);
Children.Add(navigationPage2);
}
}
}
However, they are always display in the upper part as the following image:
The previous image uses Android Oreo 8.1 (I also tested the version 7.1 and I had the same result). I got partial examples from these blogs:
https://montemagno.com/xamarin-forms-official-bottom-navigation-bottom-tabs-on-android/
https://blog.xamarin.com/xamarin-forms-3-1-improvments/
Also, the version of .NET Standard and Xamarin.Forms:
Have any of you experienced it before? Does anyone know what am I doing wrong? Thanks for your support.
Why the tabs are not in the bottom in Xamarin Forms?
I found that your were missing InitializeComponent() in your MainPage, that's why the issue happens.
The InitializeComponent() method will load the XAML content, when you delete this method, your XAML content will be ignored.
Solution:
Set Bottom Tabs in C# code behind:
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);

Sitecore using Glass mapper and MVC

Hi am using the Glass mapper in combination with Sitecore MVC. I created model below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Test
{
using Glass.Mapper.Sc.Configuration.Attributes;
[SitecoreType(AutoMap = true)]
public class GlassTestModel
{
public virtual string Title { get; set; }
}
}
this is my view
#inherits Glass.Mapper.Sc.Web.Mvc.GlassView<Test.GlassTestModel>
<div>
<h2>Hello</h2>
<h2>#Model.Title</h2>
</div>
I also tried this first line
#model Test.GlassTestModel
and this one
#inherits System.Web.Mvc.WebViewPage<Test.GlassTestModel>
I created a view rendering in Sitecore. configured the model to use above.
When I render the view the model fields don't get filled. When I test in a controller I use for other controller view like below:
ISitecoreContext ctx = new SitecoreContext();
var item = ctx.GetCurrentItem<Test.GlassTestModel>();
I do get the title field. Any ideas?
I migrated my solution to V4 of the Glass.Mapper package. This works. With V3 I can't get the automapping to work.

ASP.NET Routing all Pages

I have a route config file that I'm trying to route all URLs that follow the formula .com/{a page}/{a subpage}, to route to a specific page .com/Default/Page.aspx. My problem is that it does this for all the pages (i.e., .com/Account/Login.aspx. Is there a way to specify that I want it to route to that page only when a user types it into the address bar, possible only when they leave out the .aspx extension? This is what I have so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.Membership.OpenAuth;
using System.Web.Routing;
using Microsoft.AspNet.FriendlyUrls;
namespace CouponsForGiving
{
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.Ignore("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("LearnMore", "LearnMore", "~/LearnMore.aspx");
routes.MapPageRoute("DefaultPage", "{nponame}", "~/Default/NPOPage.aspx");
routes.MapPageRoute("CampaignPage", "{nponame}/{campaignname}", "~/Default/CampaignPage.aspx"); //This one routes a lot of other pages
routes.EnableFriendlyUrls();
}
}
}
Thanks!
Issue here is with the overriding of a route. If there are 2 routes with same number of parameters and if there is no hard-coded value, it will always consider the first route, which is declared. For Example, if following 2 routes are registered,
routeCollection.MapPageRoute("LearnMore", "{param1}/{param2}", "~/About.aspx");
routeCollection.MapPageRoute("DefaultPage", "{param3}/{param4}", "~/Account/Login.aspx");
In the above case, the route LearnMore will only be consider as valid which will be requesting About.aspx page.
You can do something like below:
routeCollection.MapPageRoute("LearnMore", "learnmore/{param1}/{param2}", "~/About.aspx");
routeCollection.MapPageRoute("DefaultPage", "default/{param3}/{param4}", "~/Account/Login.aspx");
This will redirect to the respective pages. You can go through below URL for more details on URL Routing.
http://karmic-development.blogspot.in/2013/10/url-routing-in-aspnet-web-forms-part-2.html
Thanks & Regards,
Munjal

MembershipProvider not recognized

I'm writing my own membership provider (actually just extending the built-in one) and MembershipProvider stays red with an error that it cannot be resolved, even though I've added System.Web.Security:
public sealed class MRCMembershipProvider : MembershipProvider
{
//code here
}
Anyone got any ideas why this would be happening?
Make sure that you have added reference to the System.Web.ApplicationServices.dll assembly which is where this class is defined as stated in the documentation:
Namespace: System.Web.Security
Assembly: System.Web.ApplicationServices (in System.Web.ApplicationServices.dll)
It works only when the namespace for the MVC application is present.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
namespace MvcApplication6
{
public sealed class MRCMembershipProvider : MembershipProvider
{
}
}

Resources