Skip to content

Fix S3928 FP: Should not report on Razor partial "Model" property #9796

@dougwaldron

Description

@dougwaldron

Description

A Razor partial view has a Model property. The Model type is specified by an @model directive.

If "Model" is used as a parameter in an ArgumentException, it raises an S3928 error message, "The parameter name 'Model' is not declared in the argument list" in SonarQube Cloud. (No error is generated by SonarAnalyzer.CSharp.)

(I expect this would also affect regular Razor pages, not just partial views, but I didn't test it, and it would be odd to have an ArgumentException on the Model in a Razor page anyway.)

Reproducer

Create an enum anywhere in your project:

public enum PassFail
{
    Pass,
    Fail,
}

Use that enum as the Model in a Razor partial view (e.g., "_PassFailPartial.cshtml"). Throw an ArgumentException for enum values out of range:

@model PassFail
@{
    var text = Model switch
    {
        PassFail.Pass => "Passing",
        PassFail.Fail => "Failing",
        _ => throw new ArgumentOutOfRangeException(nameof(Model)),
    };
}

<span>@text</span>

Product and Version

SonarQube Cloud

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions