Html.Editor not rendering the value

后端 未结 1 1090
轮回少年
轮回少年 2021-01-27 02:47

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

Here is the scenario:

// assign the value
    ViewBag.BeginDate = seaBeginEnd.begi         


        
1条回答
  •  自闭症患者
    2021-01-27 03:14

    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)
    

    0 讨论(0)
提交回复
热议问题