mikebrind / razor-pages-in-action Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
I'm getting a validation error for an empty claim value when I recreate this page in a project targeting .NET 7. It says: "The Claim Value field is required." The BindProperty
attribute's behavior has changed since .NET 6 and requires putting a question mark to make chapter 10 examples work.
[BindProperty, Display(Name = "Claim Value")]
public string? ClaimValue { get; set; }
Jumping straight into Chap 8 run on VS 2022 Win 10, I can right click and View in Browser Chrome https://localhost:7236/PropertyManager/Index (from the Pages ... .cshtml) to get all the City Breaks drillable to their hotels, so everything including the SQLite is working fine.
But if I similarly View in Browser the Edit.cshtml, I get a 404 Not Found error. Understandable (as no Id) but there seems to be no way to get into this editing from the above. Naturally I tried appending to the URL (from same local web session) with an actual record request similar to p237 of book (using project's /PropertyManager/ instead of quoted /property-manager/ which also doesn't work):
https://localhost:7236/PropertyManager/Edit?id=1
But same error.
The scaffolded CRUD pages and code is definitely there, so I shouldn't need to regen with VS right click "Add... Razor Pages using Entity Framework (CRUD)" as per the book when creating from scratch (which would also prob flip to SQL Server). There seems to be some routing missing but can't see anything obvious (to MVC newcomer) in CustomRouteConstraints or the Edit.cshtml.cs itself, but as there are many ways to do this, can you/anyone advise why it's not working out of the box or navigation URL or fix?
NB: Otherwise loving the book, very insightful, like most Mannings!
I can't get the CacheTagHelper example to work. It always returns a literal <vc:cities />
instead of a rendered component. But an app displays cities as soon as I change this tag helper with @await Component.InvokeAsync("Cities")
.
I'm running it on Windows 10 x64 with latest patches, VS2022 Community Edition, .NET 6.0.14
My regional settings use a comma symbol as a decimal number separator. It doesn't work with the default validator, so I had to adapt your example and redefine the jQuery Validation rule.
public class DecimalBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
var modelType = context.Metadata.UnderlyingOrModelType;
if (modelType == typeof(decimal) || modelType == typeof(decimal?))
return new BinderTypeModelBinder(typeof(DecimalBinder));
return null;
}
}
public class DecimalBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
throw new ArgumentNullException(nameof(bindingContext));
var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (valueResult == ValueProviderResult.None)
return Task.CompletedTask;
decimal result;
if (valueResult.FirstValue != string.Empty)
{
// try current culture
if (!decimal.TryParse(valueResult.FirstValue, System.Globalization.NumberStyles.Any, CultureInfo.CurrentCulture, out result) &&
// then try in US culture
!decimal.TryParse(valueResult.FirstValue, System.Globalization.NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out result) &&
// finally try neutral culture
!decimal.TryParse(valueResult.FirstValue, System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out result))
{
bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, "You should provide a valid decimal value");
return Task.CompletedTask;
}
bindingContext.Result = ModelBindingResult.Success(result);
}
else if (!bindingContext.ModelMetadata.IsNullableValueType)
{
bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, "Decimal value is non-nullable and can't be empty");
}
return Task.CompletedTask;
}
}
builder.Services.AddRazorPages().AddViewOptions(options =>
{
options.ModelBinderProviders.Insert(0, new DecimalBinderProvider());
});
Put the below code into the "/wwwroot/js/number-validator-override.js" and link it in "_ValidationScriptsPartial.cshtml" after the <script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
line.
$.validator.methods.number = function (value, element) {
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.