Html.Editor不渲染的价值 [英] Html.Editor not rendering the value

查看:172
本文介绍了Html.Editor不渲染的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在制作Html.Editor呈现的愿望HTML问题。

下面是该方案:

  //赋值
    ViewBag.BeginDate = seaBeginEnd.beginDate;//视图
    @ Html.Editor(开始,ViewBag.BeginDate为日期时间?)// HTML源代码
    < D​​IV CLASS =主编场>
    <输入类=文本框单行ID =开始NAME =开始类型=文本值=/>
    < / DIV>

我被蒙上了斑驳看到的 2011年1月19日12:00:00 AM 这是ViewBag.BeginDate的价值,任何见解的值。

感谢您的帮助!


解决方案

  

我被蒙上了斑驳地看到2011年1月19日12:00:00 AM的值,它是ViewBag.BeginDate值


您无法通过传递开始作为第一个参数 Html.Editor 帮手想到这样的事情。第二个参数并没有做什么,你认为它。它只是一些额外的视图数据发送给编辑模板,但要绑定到原来的值称为开始所以这就是你应该分配一个值。像这样的:

 公众的ActionResult指数()
{
    ViewBag.Begin = DateTime.Now;
    返回查看();
}

和则:

  @ Html.Editor(开始)

显然,我每次看到有人用ViewBag / ViewData的,而不是强类型的辅助,我觉得在推荐视图模型和强类型的辅助义务的时间。例如:

型号:

 公共类MyViewModel
{
    [DisplayFormat(DataFormatString ={0:YYYY-MM-DD},ApplyFormatInEditMode = TRUE)]
    公众的DateTime日期{搞定;组; }
}

控制器:

 公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        返回查看(新MyViewModel
        {
            日期= DateTime.Now
        });
    }
}

和相应的强类型的视图:

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

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!

解决方案

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)

这篇关于Html.Editor不渲染的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆