Search code examples
asp.net-mvcviewdata

MVC Preview 5 - ViewData/HTML Helper Quirk


The following code is in the /Courses/Detail action:

    [AcceptVerbs("GET")]
    public ActionResult Detail(int id)
    {
        ViewData["Title"] = "A View Title";
        return View(tmdc.GetCourseById(id));
    }

The tmdc.GetCourseById(id) method returns an instance of type Course for the View. In the View I am using

<%= HTML.TextBox("Title")%>

to display the value of the Title property for the Course object. Instead the text box is displaying the string A View Title. Is this normal/expected behavior? What would be the best way to handle this?

Update
As a workaround, I've changed ViewData["Title"] to ViewData["VIEW_TITLE"] but would like a cleaner way to handle this collision or to know if this is an expected result.


Solution

  • Unfortunately I'm not at my dev machine right now so I can't test this, but have you tried something like this?

    <%= Html.TextBox("Title", ViewData.Model.Title) %>