I have a proxy stub where my variable is of datetime type in the proxy stub and i am assinging that date to my view by getting it from model in need to check whether date time is null or not if null i should show not available in UI
Proxy stub
[DataMember(Order = 9)]
public System.DateTime? FromDate
{
get
{
return _FromDate;
}
set
{
this.SetDirty();
this._FromDate = value;
}
}
Model
model.FromDate = Convert.ToDateTime(MemberInfo.FromDate);
View
@if ((Model.FromDate!= null))
{
<p> @Html.DisplayFor(model => model.FromDate) </p>
}
else
{
<p> N/A</p>
}
What i tried is
if(model.FromDate!=null)
{
model.FromDate = Convert.ToDateTime(MemberInfo.FromDate);
}
else{
model.FromDate="Not Available";
}
But i am getting string to date time conversion error
if(model.FromDate!=null)
model.FromDate is actually nullable so null checking will be model.FromDate.HasValue
if model.FromDate = Convert.ToDateTime(MemberInfo.FromDate);
giving exception in non formated date strings the use tryparse
DateTime resultDate;
if (DateTime.TryParse(MemberInfo.FromDate, out resultDate))
model.FromDate="Not Available";
this cannot be done in c# as it is strongly typed so use another variable
Model
DateTime resultDate;
if (DateTime.TryParse(MemberInfo.FromDate, out resultDate))
model.FromDate.Value = resultDate;
else
model.FromDate.Value=null;
View
@if ((Model.FromDate.HasValue))
{
<p> @Html.DisplayFor(model => model.FromDate) </p>
}
else
{
<p> Not Available</p>
}
As Per Comment
model.FromDate=MemberInfo.FromDate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.