简体   繁体   English

我如何使日期字段@ Html.EditorFor接收来自ViewBag的日期

[英]How do I make the date field @Html.EditorFor receive date coming from a ViewBag

How do I make the date field @Html.EditorFor receive date coming from a ViewBag. 如何使日期字段@ Html.EditorFor接收来自ViewBag的日期。

Controller 调节器

var searchDateBD = db.Sequence.ToList().Select(l => l.DateFlow).Max();
ViewBag.date = searchDateBD;

View 视图

@Html.EditorFor(model => model.Sequence.DateFlow, 
                         ViewBag.date, 
                         new { htmlAttributes = new { @class = "form-control input-lg" } })

In this way above developed by me is not correct. 这样我上面开发的是不正确的。

How to do so that the field receives the date coming from the viewbag of the controller 如何做到这一点,以使该字段接收来自控制器的视图包的日期

You can overwrite the value using attribute @Value : 您可以使用属性@Value覆盖值:

htmlAttributes = new { @class = "form-control input-lg", @Value = ViewBag.date }

Note that @Value is written with an uppercase letter V . 请注意, @Value用大写字母V书写。

You don't need ViewBag here. 您在这里不需要ViewBag You can set the model property in the controller action and pass the model object to view back in the line returning View like: 您可以在控制器操作中设置模型属性,然后将模型对象传递回返回View的行中进行查看,例如:

model.Sequence.DateFlow = db.Sequence.Select(l => l.DateFlow).Max();

return View(model);

You also do not need to materialize the result by calling ToList() and leave the Max item to be calculates on the database server. 您也不需要通过调用ToList()来实现结果并将Max项目保留在数据库服务器上进行计算。

and in your view it would be like: 在您看来,它就像:

@Html.EditorFor(model => model.Sequence.DateFlow, 
                         new { htmlAttributes = new { @class = "form-control input-lg" } })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM