Reputation: 21
I am working on an ASP.NET Core MVC project (v.1.0.0). It is currently published to a staging and a production environment. One of the model classes was recently updated to add two new properties.
In both the development and staging environments, these new properties are working fine.
However, when deployed to the production environment the Views that reference these new properties report that they are not defined in the class. In Production:
1) Entity Framework was able to successfully implement the migration and update the database schema based on the updated class.
2) Controllers are able to interact with these new properties and even save to the database.
3) However, when using a View strongly-typed for the same class an error is thrown stating that Location does not contain a definition for Latitude.
Again, the same Views can work fine with the new properties in Development and Staging environments.
Location.cs
public class Location
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
[Display(Name = "Address Line 1")]
public string AddressLine1 { get; set; }
[Display(Name = "Address Line 2")]
public string AddressLine2 { get; set; }
[Required]
public string City { get; set; }
[Required]
public string State { get; set; }
[Required]
[Display(Name = "Postal Code")]
public int PostalCode { get; set; }
public double Latitude { get; set; } // New
public double Longitude { get; set; } // New
public string Notes { get; set; }
public Boolean IsActive { get; set; }
public Boolean IsDeleted { get; set; }
}
AddLocation.cshtml
I am using the controller to get the Latitude and Longitude on submit from an external API, which is why the fields are hidden and defaulted to 0 on the AddLocation View. The same error is thrown on other Views that are used to update or display the values.
@model MyMvcApp.Models.Location
@{
ViewData["Title"] = "Add Location";
}
<form asp-action="AddLocation">
<div class="form-horizontal">
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Latitude" value="0" />
<input type="hidden" asp-for="Longitude" value="0" />
<div class="form-group">
<label asp-for="Name" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="AddressLine1" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="AddressLine1" class="form-control" />
<span asp-validation-for="AddressLine1" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="AddressLine2" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="AddressLine2" class="form-control" />
<span asp-validation-for="AddressLine2" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="City" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="City" class="form-control" />
<span asp-validation-for="City" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="State" class="col-md-2 control-label"></label>
<div class="col-md-10">
<select asp-for="State" asp-items="@ViewBag.StateList" class="form-control"></select>
<span asp-validation-for="State" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="PostalCode" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="PostalCode" class="form-control" />
<span asp-validation-for="PostalCode" class="text-danger" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
<input asp-for="IsActive" />
<label asp-for="IsActive"></label>
</div>
</div>
</div>
<div class="form-group">
<label asp-for="Notes" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Notes" class="form-control" />
<span asp-validation-for="Notes" class="text-danger" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Add" class="btn btn-default" />
</div>
</div>
</div>
</form>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
When I remove or comment out the references to the new properties the Views render fine.
Any ideas how I can correct this?
Upvotes: 1
Views: 329
Reputation: 21
The Views behave differently than the Controllers in this situation because they are compiled at run-time rather than at build.
In my case, a dll from an earlier release was still sitting on the hosting server in a separate location. My guess, is that it was left behind from a previously attempted roll-back. Regardless, an older version of the dll was still available on the host and accessed when the Views were compiled creating the behavior.
Ideally, I should have been able to remove the outdated dll (and any related files) and restart the App Service. Unfortunately, I was having trouble finding this dll, so I took a less ideal path.
To correct this, I set the "Remove additional files at destination" checkbox in the deploy settings then published. Web Deploy Settings
NOTE: any generated or uploaded files to the web server separate from the project deployment will be removed as well. If any of these exist they will need to be generated or uploaded again.
Upvotes: 1