asp.net-mvc MVC Razor - 默认值作为文本框类型日期的当前日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26418120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:17:20  来源:igfitidea点击:

MVC Razor - Default Value as Current date for textbox type date

asp.net-mvcasp.net-mvc-4daterazor

提问by Anup

I have a Textboxwith type as date. I am trying to set default value of the textboxto current date.

我有一个Textbox类型为date. 我正在尝试设置textbox当前日期的默认值。

@Html.TextBoxFor(x => x.Date, new { @id = "Date", @type = "date", 
                                    @value = DateTime.Now.ToShortDateString() })

The above line doesn't set default value. How to set default value as current date?

上面的行没有设置默认值。如何将默认值设置为当前日期?

回答by ps2goat

As Stephen Muecke said, you need to set the property's value on the model.

正如 Stephen Muecke 所说,您需要在模型上设置属性的值。

// in controller method that returns the view.
MyModel model = new MyModel();
model.Date = DateTime.Today;

return View(model);

And your Razor would be:

你的剃刀将是:

@Html.TextBoxFor(x => x.Date, "{0:yyyy-MM-dd}", new { @class = "form-control", @type = "date"})

Note that the idand the nameproperties should be automatically assigned to the property name when using a Formethod, such as @Html.TextBoxFor(), so you don't need to explicitly set the idattribute.

请注意,在使用方法(例如 )时,应自动将idname属性分配给属性名称,因此您无需显式设置该属性。For@Html.TextBoxFor()id

回答by renjith

It's better way to manage in view

这是更好的管理方式

@Html.TextBoxFor(x=> x.Date, new { @Value = @DateTime.Now.ToShortDateString() })

回答by Radha Anil K

 $(document).ready(function () {
        var dateNewFormat, onlyDate, today = new Date();

        dateNewFormat = today.getFullYear() + '-';
        if (today.getMonth().length == 2) {

            dateNewFormat += (today.getMonth() + 1);
        }
        else {
            dateNewFormat += '0' + (today.getMonth() + 1);
        }

        onlyDate = today.getDate();
        if (onlyDate.toString().length == 2) {

            dateNewFormat += "-" + onlyDate;
        }
        else {
            dateNewFormat += '-0' + onlyDate;
        }

        $('#mydate').val(dateNewFormat);
    });

回答by Pamy

<input asp-for="date" value="@DateTime.Today" type="datetime" class="form-control" />

<input asp-for="date" value="@DateTime.Today" type="datetime" class="form-control" />

This works for me. Remember to change 'date' according to your model.

这对我有用。请记住根据您的型号更改“日期”。

and use this, if you need time as well

并使用它,如果你也需要时间

<input asp-for="date" value="@DateTime.Now" type="datetime" class="form-control" />

<input asp-for="date" value="@DateTime.Now" type="datetime" class="form-control" />

回答by Pamy

Another Solution:

另一个解决方案:

 @Html.TextBoxFor(model=>model.CreatedOn, new{@value= System.DateTime.Now})

It works on my end, sure it will work on yours.

它对我有用,肯定对你有用。