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") })
Related
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?
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" })
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.
In my MVC application, I am retrieving a list of objects based on an ID and stuffing those objects into a Listbox via a SelectList. Here is what I have:
C#
ViewBag.SpecCatListBox = new SelectList(SelectListMethods.LstChosenSpecCat(incidentVm.ID), "Value", "Text");
HTML/Razor
#Html.ListBoxFor(model => model.LstSpecialCategories, (SelectList)ViewBag.SpecCatListBox, new { id = "SpecialCat-ListBox", #class = "form-control" })
On page load, the listbox is filled with the correct options, except that they're not selected. Is there a way to do this without looping (which is what I've seen in other posts)?
Any help is appreciated.
UPDATE
I've edited a few things, along with added a line of code.
C#
ViewBag.SpecCatListBox = new SelectList(SelectListMethods.LstChosenSpecCat(incidentVm.ID), "Value", "Text", SelectListMethods.LstChosenSpecCat(incidentVm.ID));
ViewBag.SpecCatIds = db.TBL_AssocIncidentSpecialCat.Where(x => x.IncidentId == incidentVm.ID)
.Select(x => x.SpecialCategoriesId).ToList();
HTML/Razor
#Html.ListBoxFor(model => model.LstSpecialCategories, new MultiSelectList(ViewBag.SpecCatListBox, "Value", "Text", ViewBag.SpecCatIds), new { id = "SpecialCat-ListBox", #class = "form-control" })
This is selecting all of the options as needed, but is there a way to not use 2 Viewbag objects?
I have figured this out.
C#
ViewBag.SpecCatListBox = new MultiSelectList(SelectListMethods.LstChosenSpecCat(incidentVm.ID), "Value", "Text", SelectListMethods.LstChosenSpecCat(incidentVm.ID).Select(x => x.Value));
HTML/Razor
#Html.ListBoxFor(model => model.LstSpecialCategories, (MultiSelectList)ViewBag.SpecCatListBox, new { id = "SpecialCat-ListBox", #class = "form-control" })
I am using the code below to make the text entry read only but this does not become read only any idea why this could be
#Html.EditorFor(model => model.UserName, new { Readonly = "Readonly"})
Why cont you use
#Html.EditorFor(model => model.UserName, new { #readonly = true});
or you can use like
#Html.EditorFor(model => model.UserName, new { #readonly = "readonly"});
try textboxfor
#Html.TextBoxFor(model => model.UserName, new { #readonly = true})
The HtmlHelper EditorFor do not have overloads that take HTML attributes.
Instead of EditorFor go with TextBoxFor
#Html.TextBoxFor(model => model.UserName, new { disabled = "disabled", #readonly = "readonly"})
hope it helps