I'd like to set a default value to my model in Controller, But It cannot display in create page.
我想在Controller中为我的模型设置一个默认值,但是它无法在创建页面中显示。
TestModel code:
public class TestModel
{
[DataType(DataType.DateTime), Required]
[DisplayFormat(DataFormatString = "yyyy/MM/dd", ApplyFormatInEditMode = true)]
public DateTime StartTime { get; set; }
[DataType(DataType.DateTime), Required]
[DisplayFormat(DataFormatString = "yyyy/MM/dd", ApplyFormatInEditMode = true)]
public DateTime EndTime { get; set; }
public string Description { get; set; }
}
Controller code:
public ActionResult Create()
{
var model = new TestModel();
model.StartTime = DateTime.Now;
model.EndTime = DateTime.Now.AddDays(10);
model.Description = "This is a default value";
return View(model);
}
View page:
<div class="form-group">
@Html.LabelFor(model => model.StartTime, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StartTime)
@Html.ValidationMessageFor(model => model.StartTime)
</div>
</div>
But the display is not correct that it is has no default datetime
value, but the description default value is display correct:
但显示不正确,它没有默认的日期时间值,但描述默认值显示正确:
4 个解决方案
#1
3
You need to have model class property like below :
您需要具有如下所示的模型类属性:
[DataType(DataType.DateTime), Required]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime StartTime { get; set; }
[DataType(DataType.DateTime), Required]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime EndTime { get; set; }
When you decorate a model property with [DataType(DataType.Date)]
the default template in ASP.NET MVC
generates an input field of type="date"
.
使用[DataType(DataType.Date)]修饰模型属性时,ASP.NET MVC中的默认模板会生成type =“date”的输入字段。
#2
0
Change the model to :
将模型更改为:
public class TestModel
{
[DataType(DataType.DateTime), Required]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime StartTime { get; set; }
[DataType(DataType.DateTime), Required]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime EndTime { get; set; }
public string Description { get; set; }
}
it is figure out. cause the display format string is not correct.
它是搞清楚的。导致显示格式字符串不正确。
#3
0
Your razor would be as follows:
你的剃刀如下:
@Html.TextBoxFor(x => x.Date, "{0:yyyy-MM-dd}", new { @class = "form-control",@type = "date"})
#4
-1
Use DataAnnotations to st default values:
将DataAnnotations用于st默认值:
[DefaultValue(System.DateTime.Now)]
public DateTime DateTo { get; set; }
#1
3
You need to have model class property like below :
您需要具有如下所示的模型类属性:
[DataType(DataType.DateTime), Required]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime StartTime { get; set; }
[DataType(DataType.DateTime), Required]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime EndTime { get; set; }
When you decorate a model property with [DataType(DataType.Date)]
the default template in ASP.NET MVC
generates an input field of type="date"
.
使用[DataType(DataType.Date)]修饰模型属性时,ASP.NET MVC中的默认模板会生成type =“date”的输入字段。
#2
0
Change the model to :
将模型更改为:
public class TestModel
{
[DataType(DataType.DateTime), Required]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime StartTime { get; set; }
[DataType(DataType.DateTime), Required]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime EndTime { get; set; }
public string Description { get; set; }
}
it is figure out. cause the display format string is not correct.
它是搞清楚的。导致显示格式字符串不正确。
#3
0
Your razor would be as follows:
你的剃刀如下:
@Html.TextBoxFor(x => x.Date, "{0:yyyy-MM-dd}", new { @class = "form-control",@type = "date"})
#4
-1
Use DataAnnotations to st default values:
将DataAnnotations用于st默认值:
[DefaultValue(System.DateTime.Now)]
public DateTime DateTo { get; set; }