This is my first question posted here, so forgive me if something is missing. I will update any requested information.
Explanation
I am working on my first C # .Net MVC-3 application. I am using Entity Framework 4.0 and the Razor viewer.
In most views, I use a view model containing instances of a partial class. This partial class is derived from EF classes and contains data annotations for these class members. This worked pretty smoothly, and now all my views that use this view model have validation for the affected text fields. Examples:
Partial Class:
[MetadataType(typeof(DataPackageMetaData))] public partial class DataPackage {
ViewModel:
public class PackageInfo {
So, all the verification materials for this material "just work." In the view (which is actually part of the partial view), I just put "TextBoxFor". and then if the check fails, a good error message is posted in red:
View:
@Html.TextBoxFor(model => Model.package.DataPackageID, new { @class = "short", @id = "DataPackageID" }) @Html.ValidationMessageFor(model => Model.package.DataPackageID, "The Data Package ID is required and cannot be longer than 50 characters")
Problem
The problem that I am facing now is that I switched to the reporting aspect of the program, I want the new ViewModel to be able to pass the values ββof the "DateIn" and "DateOut" forms (among other things) so that user-generated report queries can be generated. I want to be able to verify that DateIn and DateOut are indeed date values. For some reason this does not work.
ViewModel:
public class PackageVals { < ... > // Date value for formatted "Date In" value [DataType(DataType.Date)] [DisplayName("Date In")] [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] [Required] public DateTime DateIn { get; set; } // Date value for formatted "Date Out" value [DataType(DataType.Date)] [DisplayName("Date Out")] [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] [Required] public DateTime DateOut { get; set; } <... more stuff ...> }
View
@Html.TextBoxFor(model => Model.DateIn, new { @class = "small", @id = "DateIn"}) @Html.ValidationMessageFor(model => Model.DateIn, "A valid date must be entered in the format mm/dd/yyyy.") <...> @Html.TextBoxFor(model => Model.DateOut, new { @class = "small", @id = "DateOut"}) @Html.ValidationMessageFor(model => Model.DateOut, , "A valid date must be entered in the format mm/dd/yyyy.")
Please note that in all cases I use partial views, which are then embedded in other views. There are several different views in the application reporting section, and almost all of them will need the values ββof the "DateIn" and "DateOut" forms.
I'm running out of things to look at and the time to look at them. Right now, I'm just mistakenly mistaken for the date value in the controller and pass a null ViewModel so that the user does not have a yellow death screen.
I know that validation is based on jQuery, but I don't think I'm doing anything there that might cause the problem. Here is all my jQuery script:
jQuery Script
$(document).ready(function () { // Purple box autocomplete Stuff $("#Product").autocomplete({ source: '/SharedFunctions/AutoFillProduct' }); $("#ActiveIngredient").autocomplete({ source: '/SharedFunctions/AutoFillActiveIngredient' }); $("#Applicant").autocomplete({ source: '/SharedFunctions/AutoFillApplicant' }); // Purple box autocomplete Stuff $("#DataType").autocomplete({ source: '/SharedFunctions/AutoFillDataType' }); // Purple Box datepicker stuff $("#DateReceived").datepicker({ dateFormat: "mm/dd/yy" }); $("#OutDate").datepicker({ dateFormat: "mm/dd/yy" }); // Silver box datepicker stuff; Also used by Reports $("#DateIn").datepicker({ dateFormat: "mm/dd/yy" }); $("#DateOut").datepicker({ dateFormat: "mm/dd/yy" }); // Alternate table row colors $("table.center > tbody tr:even").css("background-color", "#F2F8FF"); });
Any help you could tell me would be wonderful. Let me know if you need anything else.
Thanks again.
Additional Information
I wanted to clarify if there is any confusion: I have two ViewModels models: The first ViewModel (called PackageInfo) contains instances of objects created from the Entity Framework model. Values ββin PackageInfo are correctly validated in the view by a "simple" MVC check.
The second ViewModel (called PackageVals) contains DateTime values. Values ββin PackageVals are not checked in the view.