I have a view where there are ajax.actionlinks, some of these action links need to display a date property of the model and I have the date property as follows:
[Display(Name = "Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
public DateTime? Date { get; set; }
however, because ajax.actionlink accepts a string for its first argument, I can't use a lambda expression :
m => m.Date
rather I'm using
Model.Date.ToString()
but this isn't showing the formatting I want. I've tried doing
Model.Date.ToString("MM-dd-yyyy");
but I'm getting red underline because its not recognizing the ToString overload with 1 argument... any ideas on how I can get this to work?
Since Model.Date is nullable, you need to access the Value of the DateTime? before using that version of ToString:
Model.Date.HasValue ? Model.Date.Value.ToString("MM-dd-yyyy") : null;
Related
I have my date data annotation as
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime CreatedOn { get; set; }
In my view:
#Html.DisplayFor(item=> item.CreatedOn)
But my date appears as just: 11 12 2017 in my view, insteaed of 11/12/2017. What ate my /'s? Anything I forgot to include?
In the format-string, wrap the / in single quotes, so your model should look something like this:
[DisplayFormat(DataFormatString = "{0:dd'/'MM'/'yyyy}")]
public DateTime CreatedOn { get; set; }
When rendered on the page, it uses the desired format.
The Documentation on DataFormatString has a remark about formatting of dates, but doesn't mention anything about this issue of formatting forward-slashes. Their proposed solution about setting HtmlEncode = true didn't work for me. I found the solution in the alternative suggestion on the answer for this similar question.
It seems everything boils down to Culture info. As it currently stands it doesn't seem like we can specify CultureInfo in DisplayFormat, so i ended up defining a reusable helper method:
public static string FormatDate(this IHtmlHelper helper, DateTime date)
{
var formattedDate = string.Format(CultureInfo.InvariantCulture, "{0:dd/MM/yyyy}", date);
return formattedDateWithTime;
}
and in my view:
#Html.FormatDate(Model.CreatedOn)
I have a problem with datepicker format, it sets the date to MM-dd-yyyy. First, my model was set to following date format dd-MM-yyyy, I've changed it to MM-dd-yyyy in my model but nothing happen, the jqueryval script throw an error it wants the dd-MM-yyyy format.
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
public DateTime? dateOut_cash
{
get;
set;
}
So how can I set it to have everything working well ?
I have an ASP.NET MVC application with a model with the following property:
[DisplayFormat(DataFormatString = "{0:N1}", ApplyFormatInEditMode = true)]
public List<decimal?> Scores { get; set; }
I was expecting it to be formatted with one decimal when printed like this:
#Html.EditorFor(p => p.ContainingModelList[i].Scores[j])
It is not. It shows two decimals no matter what. If I try to implement a dummy property like this, formatting works though:
[DisplayFormat(DataFormatString = "{0:N1}", ApplyFormatInEditMode = true)]
public decimal? Test { get; set; }
// Test is later initalized with 1.35443M and rendered as "1.4"
So... does the DisplayFormat attribute in combination with EditorFor not support lists? Any way to work around it?
edit: I meant EditorFor, but wrote DisplayFor
I have this code:
#Html.TextBoxFor(m => Model.MyDateTime)
MyDateTime - is DateTime object.
It shows correct date and time inside textbox: 09/10/2010 05:19:56 PM
But when I try to click submit button it shows that it is incorrect value. I use jquery.validate.unobtrusive.js file for validation.
The gist of the solution I pointed to in my comment is that you can use a specialized model for the view which contains a string representation instead of the DateTime type, which allows you to easily validate the value with RegularExpressionAttribute. When you receive this model on the server (as posted from the client), simply convert it to a corresponding database model.
public class ViewModel
{
[Required]
[RegularExpression("\d{2}-\d{2}-\d{4}\s\d{2}:\d{2}:\d{2}")]
public string MyDateTime { get; set; }
public Model ToPoco()
{
return new Model {
MyDateTime = DateTime.Parse(this.MyDateTime, "MM-dd-yyyy H:mm:ss")
};
}
}
public class Model
{
DateTime MyDateTime { get; set; }
}
data annotation will work for you!
You could use dataannotaion for validate yor model field properly. Using such annatation you could manualy prvide format of date in your annotation passing string pattern to it. And in that case it will perefectly working with default mvc validation.
I have a model with a DateTime propery:
[DisplayName("Updated")]
public DateTime lastUpdatedDate { get; set; }
At the moment, I think I am incorrectly handling the formatting of the datetime in the view.
<tr>
<td>#Html.LabelFor(m=>m.lastUpdatedDate)</td>
<td>#Html.Label(Model.lastUpdatedDate.ToLongDateString())</td>
</tr>
I am sure this is wrong. Firstly, should I do the formatting in the model, and return string (In the model used for displaying the date - the Update model needs the DateTime type for the control)? But it gets complicated - timezones. Should I manipulate the value of the date time (based on a timezone selection by the user on registration) in the model on the get; .. thing? (What's the called? The getter?? hehe).
Just trying to make my code friendly to work with, while I learn MVC.
If you want to elegantly deal with timezones, I suggest you read this answer. For simple formatting the DateTime property in your model, decorate it with the [DisplayFormat] attribute:
[DisplayName("Updated")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime lastUpdatedDate { get; set; }
and in your view:
#Html.DisplayFor(x => x.lastUpdatedDate)