C # MVC 3 Checking for non-compliance with individual views

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 { //empty } public class DataPackageMetaData { [Required] [StringLength(50)] public string DataPackageID { get; set; } ... (a lot more stuff like this) ... } 

ViewModel:

 public class PackageInfo { // Registration Package information public DataPackage package { get; set; } public Reviewer reviewer { get; set; } public PackageTracking trackingSave { get; set; } <...> //Constructor public PackageInfo() { package = new DataPackage(); reviewer = new Reviewer(); trackingSave = new PackageTracking(); } } 

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.

+4
source share
2 answers

The problem ended up being in the controller, this is the only code that I did not post above. I think if I published this initially, then @Mystere Man, or any other person who was kind enough to try to help me, immediately picked it up.

What happened was that I made the argument of the Controller function to the FormCollection variable, and not to the model variable, therefore, as a result, the data was transferred without checking for model verification.

Here is the code that works:

  [HttpPost] public ActionResult RptPkgsRecd(PackageVals model) { ViewBag.CurrentPage = "reports"; try { if (ModelState.IsValid) { PackageVals pv = new PackageVals(); pv.dps = regpacksRepository.GetSubmitted(model.datePass.DateIn, model.datePass.DateOut).ToList(); return View(pv); } else { PackageVals pv = null; return View(pv); } } catch(SystemException ex) { var error = ex.ToString(); this.ModelState.AddModelError(error, ex); PackageVals pv = null; return View(pv); } } 
0
source

Make the DateTime Nullable. If you need them to be needed, make sure they have a required attribute on them.

If your objects cannot be nullified, you need a view model that is separate from your data model. ViewModel allows you to customize it for the needs of the presentation, regardless of the needs of the data model.

0
source

Source: https://habr.com/ru/post/1414022/


All Articles