GithubHelp home page GithubHelp logo

razor-pages-in-action's People

Contributors

mikebrind avatar stepcomms avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

razor-pages-in-action's Issues

[Chapter 10] Claims manager assign page in .NET 7

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; }

404 Not Found on https://localhost:XXXX/PropertyManager/Edit?id=1 (or property-manager etc)

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!

[Chapter 14] Cache tag helper doesn't work

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

Decimal type comma separator

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);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.