ASP.NET MVC 4 不会为日期类型的输入字段呈现正确的日期格式

ASP.NET MVC 4 does not render correct date format for input field of type date

提问人:Ezra 提问时间:9/21/2023 最后编辑:Ezra 更新时间:9/22/2023 访问量:19

问:

显示的日期是 ,我的区域设置是 ,这就是我想要的。我试图将文化融入到它自身和的行动中。 还尝试了使用 BindModel 的解决方案,但没有成功。MM/dd/yyyydd/MM/yyyyglobal.asaxApplication_Start()

enter image description here

我的模型类:

public class report_parametersVM
{
     public string fromHeshbon { get; set; } = " ";
     public string toHeshbon { get; set; } = "999999999";

     [Required]
     [DataType(DataType.Date)]
     public DateTime fromDate { get; set; }
     
     [Required]
     [DataType(DataType.Date)]
     public DateTime toDate { get; set; }
}

我的控制器索引方法:GET

    [HttpGet]
    public ActionResult Index()
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("he-IL");
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("he-IL");
        report_parametersVM model = new report_parametersVM();
        model.fromHeshbon = "";
        model.toHeshbon = "9999999999";
        model.fromDate= DateTime.Now;
        model.toDate = DateTime.Now;

        return View(model);
    }

我的观点:

@model QuerySiteMVC.Models.report_parametersVM
@{
    ViewBag.Title = "Home Page";
}

<main>
    <div class="row">
        <h4 id="title mb-3">exporting 2231 report from eshkol to excel</h4>
            @using (Html.BeginForm("Index", "Home", new { fromHeshbon = Model.fromHeshbon, toHeshbon = Model.toHeshbon, fromDate = @Model.fromDate, toDate = @Model.toDate }, FormMethod.Post))
            {
                <div class="input-group mb-3">
                    <span class="input-group-text col-md-2">From Heshbon</span>
                    @Html.TextBoxFor(model => model.fromHeshbon, new { @class = "form-control" })
                </div>
                <div class="input-group mb-3">
                    <span class="input-group-text col-md-2">To Heshbon</span>
                    @Html.TextBoxFor(model => model.toHeshbon, new { @class = "form-control" })
                </div>
                <div class="input-group mb-3">
                    <span class="input-group-text col-md-2">From Date</span>
                    @Html.TextBoxFor(model => model.fromDate, "{0:yyyy-MM-dd}", new { type = "date", @class = "form-control", value = @Model.fromDate })
                </div>
                <div class="input-group mb-3">
                    <span class="input-group-text col-md-2">To Date</span>
                    @Html.TextBoxFor(model => model.toDate, "{0:yyyy-MM-dd}", new { type = "date", @class = "form-control", value = @Model.toDate })
                </div>
                <input type="submit" value="Download" class="btn btn-primary" />
            }
    </div>
</main>
asp.net asp.net-mvc-4 cultureinfo

评论


答: 暂无答案