MVC辅助类参数问题 [英] MVC helper class parameter issue
本文介绍了MVC辅助类参数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在传递参数,辅助类的问题
i have an issue in passing parameter to helper class
我的模型
public DateTime? dTime { get; set; }
Helper类由达林季米特洛夫回答
Helper class as answered by Darin Dimitrov
public static IHtmlString MyFunction(this HtmlHelper html, DateTime value)
{
return new HtmlString(value.ToString("dd/MM/yyyy"));
}
和在MyView的访问我要转换的datetime
and am accessing in myview to convert datetime
foreach (var item in Model.lstCommet)
{
<div class="comment_time">@Html.MyFunction(item.dTime)</div>
}
但我得到ASP.DetailPageHelper.convertTime(System.DateTime的)'有一些无效参数
我在做什么错了?
推荐答案
由于您需要参考值的可空类型。
Because it's a nullable type you need to reference the value.
foreach (var item in Model.lstCommet)
{
<div class="comment_time">@Html.MyFunction(item.dTime.Value)</div>
}
您可能想运行一个空检查为好。
You might want to run a null check as well.
foreach (var item in Model.lstCommet)
{
if(item.dTime.HasValue)
{
<div class="comment_time">@Html.MyFunction(item.dTime.Value)</div>
}
}
这篇关于MVC辅助类参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文