Persian Calender in MVC , Asp.net

旧时模样 提交于 2019-12-04 13:03:46

问题


I use a DateTime variable in MVC and I want to show Persian calender for

@Html.EditorFor(x=> x.ProductionDate)

How can I do it?


回答1:


I found the solution

1.Go to www.amib.ir/weblog/?page_id=316 and download the latest version of "AMIB_jsPersianCal"


2.Add "js-persian-cal.min.js" and "js-persian-cal.css" and "pcal.png" to your project
you can change the css for specified the Url of PNG file

3.Add the css and js file to your cshtml file

<link href="@Url.Content("~/Content/js-persian-cal.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/js-persian-cal.min.js")"></script>


4.change your date file like

@Html.TextBoxFor(m => m.Birthdate, new { @id = "pcal1", @class = "pdate" });

note: I use string variable for Birthdate


5.add the script to the end of cshtml File

<script type="text/javascript">
var objCal1 = new AMIB.persianCalendar('pcal1'); </script>



回答2:


Try this template (datetime.cshtml) to show/convert DateTime to PerisanDate automatically:

@*
  Copy this file to:
  Views\Shared\DisplayTemplates\datetime.cshtml
*@

@using System.Globalization
@model Nullable<DateTime>

@helper ShamsiDateTime(DateTime info, string separator = "/", bool includeHourMinute = true)
{
    int ym = info.Year;
    int mm = info.Month;
    int dm = info.Day;
    var sss = new PersianCalendar();
    int ys = sss.GetYear(new DateTime(ym, mm, dm, new GregorianCalendar()));
    int ms = sss.GetMonth(new DateTime(ym, mm, dm, new GregorianCalendar()));
    int ds = sss.GetDayOfMonth(new DateTime(ym, mm, dm, new GregorianCalendar()));    
    if (includeHourMinute)
    {
        @(ys + separator + ms.ToString("00") + separator + ds.ToString("00") + " " + info.Hour + ":" + info.Minute)
    }
    else
    {
        @(ys + separator + ms.ToString("00") + separator + ds.ToString("00"))
    }
}

@if (@Model.HasValue)
{
  @ShamsiDateTime(@Model.Value , separator: "/", includeHourMinute: false)
}

Also if you are looking for the Persian date time pickers, take a look at: http://www.dotnettips.info/newsarchive/details/1122




回答3:


try this in web.config:

 <system.web>
     <globalization culture="fa-IR" uiCulture="fa-IR" 
     requestEncoding="utf-8" 
     responseEncoding="utf-8" />
 </system.web>


来源:https://stackoverflow.com/questions/16248909/persian-calender-in-mvc-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!