Html.Editor 不渲染值

发布于 2024-10-14 14:14:50 字数 478 浏览 3 评论 0原文

我在使 Html.Editor 渲染所需的 HTML 时遇到问题。

以下是场景:

// assign the value
    ViewBag.BeginDate = seaBeginEnd.beginDate;

//View
    @Html.Editor("Begin", ViewBag.BeginDate as DateTime?)

//HTML Source
    <div class="editor-field">
    <input class="text-box single-line" id="Begin" name="Begin" type="text" value="" />
    </div>

我想看到 1/19/2011 12:00:00 AM 的值,这是 ViewBag.BeginDate 的值,任何见解。

感谢您的帮助!

I'm having problems making the Html.Editor rendering the desire HTML.

Here is the scenario:

// assign the value
    ViewBag.BeginDate = seaBeginEnd.beginDate;

//View
    @Html.Editor("Begin", ViewBag.BeginDate as DateTime?)

//HTML Source
    <div class="editor-field">
    <input class="text-box single-line" id="Begin" name="Begin" type="text" value="" />
    </div>

I was specking to see a value of 1/19/2011 12:00:00 AM which is the value of ViewBag.BeginDate, any insights.

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌入人心 2024-10-21 14:14:51

我发现值是 1/19/2011 12:00:00 AM,这是 ViewBag.BeginDate 的值

您不能通过将 Begin 作为第一个参数传递给 来期望这样的事情Html.Editor 帮助器。第二个参数并没有像你想象的那样做。它只是将一些额外的视图数据发送到编辑器模板,但您绑定到的原始值称为 Begin,因此您应该为其分配值。像这样:

public ActionResult Index()
{
    ViewBag.Begin = DateTime.Now;
    return View();
}

然后:

@Html.Editor("Begin")

显然,每次我看到有人使用 ViewBag/ViewData 而不是强类型助手时,我都觉得有义务推荐视图模型和强类型助手。示例:

模型:

public class MyViewModel
{
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime Date { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel
        {
            Date = DateTime.Now
        });
    }
}

以及相应的强类型视图:

@model AppName.Models.MyViewModel
@Html.EditorFor(x => x.Date)

I was specking to see a value of 1/19/2011 12:00:00 AM which is the value of ViewBag.BeginDate

You cannot expect such thing by passing Begin as first parameter to the Html.Editor helper. The second parameter doesn't do what you think it does. It simply sends some additional view data to the editor template but the original value you are binding to is called Begin so that's what you should assign a value to. Like this:

public ActionResult Index()
{
    ViewBag.Begin = DateTime.Now;
    return View();
}

and then:

@Html.Editor("Begin")

Obviously every time I see someone using ViewBag/ViewData and not strongly typed helpers I feel in the obligation to recommend view models and strongly typed helpers. Example:

Model:

public class MyViewModel
{
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime Date { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel
        {
            Date = DateTime.Now
        });
    }
}

and the corresponding strongly typed view:

@model AppName.Models.MyViewModel
@Html.EditorFor(x => x.Date)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文