Haim Katz
Haim Katz

Reputation: 461

MVC Routing with Period in parameter name

I am trying to implement a remote validation using entity framework in an MVC application. I need help trying to define the action signature and the appropriate route config. I have the following class in my model:

 public class FiscalReports
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long id { get; set; }
    public Int64 Counter { get; set; }
    public short FiscalYear { get; set; }
   [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MMM dd, yyyy}")]
   [DisplayFormat(DataFormatString = "${0:N0}")]
    [Remote("ValidateSalary", "FiscalReports", AdditionalFields ="Counter, FiscalYear")]
    public int? Salaries { get; set; }

}

I have a viewmodel which is used for a view that contains several of the above objects.

 public class FiscalReportVM
{
    public FiscalReports CurrentFR { get; set; }
    public FiscalReports ReportedToDate { get; set; }
    public FiscalReports BudgetToDate { get; set; }
}

The Validation action is in the FiscalReports controller is as follows:

  public JsonResult ValidateSalary(int Salaries, short FiscalYear, int Counter)
    {
        return ValidateFiscalField(Salaries, FiscalYear, Counter, "Salaries");
    }

In the view I am using the HTML helper

@Html.EditorFor(model=>model.CurrentFR.Salaries)

This generates the field and validation correctly. Generated HTML is below

input data-val="true" data-val-number="The field Salaries must be a number." data-val-remote="'Salaries'; is invalid." data-val-remote-additionalfields="*.Salaries,*.Counter,*.FiscalYear" data-val-remote-url="/FiscalReports/ValidateSalary" name="CurrentFR.Salaries" type="number" value="" />

The validation request is firing properly and in fiddler I see the following request:

http://localhost:50409/FiscalReports/ValidateSalary?CurrentFR.Salaries=27000&CurrentFR.Counter=4773&CurrentFR.FiscalYear=2

My problem is that I have trouble defining a route and action with the variables in dotted notation. The action definition does not accept dotted parameters (Can't do ValidateSalary(int CurrentFR.Salaries,....). I need help trying to define the action signature and the appropriate route config.

Upvotes: 0

Views: 422

Answers (1)

user7435243
user7435243

Reputation: 41

Can't you just use a bit of JQuery to change the name attribute? Something to the effect of:

$("CurrentFR.Salaries").attr('name', 'Salaries')

Remember having a not dissimilar issue and I just temporarily changed the name in the view, and then changed in back in the action.

Upvotes: 1

Related Questions