MVC5 Validation for DateTime in German in a razor view - c#

I have a problem with the valitation of the DataType.Date in a GERMAN format 0:dd.MM.mm.yyyy.
Model
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "LogEntry")]
public DateTime LogEntry { get; set; }
View
#Html.LabelFor(model => model.LogEntry)
#Html.TextBoxFor(model => model.LogEntry, "{0:dd.MM.mm.yyyy}", new { #class = "form-control" , #Value = #DateTime.Now })
#Html.ValidationMessageFor(model => model.LogEntry, "", new { #class = "text-danger" })
The day value is changed in the valitation with the month value. Is that the reason why the validation release??
This picture shows the day value bigger than 12
Message in english: "The Edit Time field must be a date"
This picture shows the day value lesser than 13
#Html.ValidationMessageFor(model => model.LogEntry, "", new { #class = "text-danger" })
Here are my questions.
Does someone knows where the default messages of the validation comes from ?
Does someone knows where I can switch of the validation?
Does someone knows how to solve the problem?

Related

Helping user input correct format into EditFor

My SQL database has a column Date which allows strings only in such format: __/____, basically month/year for example: 01/2019.
Is it possible for my:
#Html.EditorFor(model => model.Date, new { htmlAttributes = new { #class = "form-control" } })
to have some helper so that user can type only using the correct format? For example the editor which has strong typed something like this: __/___ and can only input numbers to fill underscores.
#Html.TextBoxFor(m => m.StartDate,
new { #Value = Model.StartDate.ToString("yyyy/MM/dd"), #class="datepicker" })

Why isn't my EditorFor datepicker field populating my value?

I'm trying to get this datepicker field to populate with today's date but it's not happening for some reason. I can't find a reference for what options are accepted in the "new {}" section.
#Html.LabelFor(d => d.ServiceOn, new { #class = "control-label" })
#Html.EditorFor(d => d.ServiceOn, "DatePicker", new { disableMinDate = true, Value = DateTime.Today })
#Html.ValidationMessageFor(d => d.ServiceOn, "", new { #class = "text-danger" })
I've tried value, Value and #Value but the resulting html always shows value="". I'm wondering if maybe the datepicker itself is zeroing out the field. I feel like I'm missing something obvious here.
This should work:
#Html.LabelFor(d => d.ServiceOn, new { #class = "control-label" })
#{Model.ServiceOn= DateTime.Today;}
#Html.EditorFor(d => d.ServiceOn, "DatePicker", new { disableMinDate = true })
#Html.ValidationMessageFor(d => d.ServiceOn, "", new { #class = "text-danger" })
HTML work with the ModelState, not from the model itself.
If you want more loose helpers, ise #Html.Editor instead.

How to hide #Html.DropDownList if ViewBag is null or empty

I have #Html.DropDownList which take data from controller in edit mode. I need to hide DropDown element and show some message if list is null or empty.
I try with this code in view, but all the time give me result thet have, and show empty dropdown:
#if(ViewBag.DatumRID != null)
{
<div class="col-md-10">
#Html.DropDownList("DatumRID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.DatumRID, "", new { #class = "text-danger" })
</div>
}
else
{
<h6 style="color:#ff0000"> NO RECORDS.</h6>
}
And code from controller is here:
ViewBag.DatumRID = new SelectList(db.tbl_relacii.Where(x => x.DatumR == tbl_rezervacii.DatumP).OrderBy(x => x.DatumR), "relID", "DatumForDisplay", tbl_rezervacii.DatumRID);
when record fount dropdown is ok, but when record is null dropdown show empty.
Check list size also. For correct displaying data SelectList must contain more than zero items. Try this:
#if(ViewBag.DatumRID != null && ViewBag.DatumRID.Count > 0)
{
<div class="col-md-10">
#Html.DropDownList("DatumRID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.DatumRID, "", new { #class = "text-danger" })
</div>
}
else
{
<h6 style="color:#ff0000"> NO RECORDS.</h6>
}
Update:
U may try update your controller code like this:
List<SelectListItem> viewList = new List<SelectListItem>();
viewList.AddRange(new SelectList(db.tbl_relacii.Where(x => x.DatumR == tbl_rezervacii.DatumP).OrderBy(x => x.DatumR), "relID", "DatumForDisplay", tbl_rezervacii.DatumRID));
ViewBag.DatumRID = viewList;
And pass "viewList" object to DropDownList helper at razor markup.
Ok. I searched around, and could find anything reasonable to work with. Selectlists don't have a Count() or any() method like most lists. Here's what I came up with:
#{
var counter=0;
foreach(var a in Viewbag.DatumRID)
{
counter++;
break; //stop it so it doesn't iterate through the whole thing since that's unnecessary
}
}
#if(counter>0){//put your dropdown here}

regularExpression with decimals in model for html.textboxfor

I am having issues with html.textboxfor when i have to insert a decimal into my db, which is also set to a decimal.
i have tried this:
[RegularExpression(#"^[0-9]+(\.[0-9]{1,3})$", ErrorMessage = "Valid Decimal number with maximum 3 decimal places.")]
but it will only accept it if i have at least 3 decimals (1,123).
i need it to be able to accept, {1 - 1,2 - 1,23 - 1,234}
how do i achieve this ?
i have not been able to find a regularexpression generator which i could figure out how to use..
or am i in completely the wrong direction as to how i am going to solve my issue?
my value in the model:
[RegularExpression(#"^[0-9]+(\.[0-9]{1,3})$", ErrorMessage = "Valid Decimal number with maximum 3 decimal places.")]
[Required]
public decimal Average { get; set; }
input html in my form:
<div class="form-group">
#Html.LabelFor(x => x.Average, "Gennemsnit")
#Html.TextBoxFor(x => x.Average, new { #class = "form-control" })
#Html.ValidationMessageFor(x => x.Average, "", new { #class = "text-danger" })#
</div>
Update
It was indeed as StephenMuecke said, that i needed to change $.validator in jquery validator.
so i added this:
(function () {
$.validator.methods.number = function (value, element) {
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:.\d{3})+)?(?:\,\d+)?$/.test(value);
}
})();
to overwrite the method.

MVC View - Date Format Issue

While Binding data to TextBox, I want to convert it to dd/MM/yyyy format. In the Following Code, HolidayDate can be in any format which comes from db.
#Html.TextBoxFor(m => m[i].HolidayDate, new { #class = "datepicker", #value = #Model[i].HolidayDate.ToString("dd/MM/yyyy") })
The date doesn't get converted to dd/MM/yyyy. How to Convert the date in a particular format?
#Html.TextBoxFor(m => m[i].HolidayDate, "{0:dd/MM/yyyy}", new { #class = "datepicker", #value = #Model[i].HolidayDate.ToString("dd/MM/yyyy") })

Categories

Resources