tags:

views:

473

answers:

1

I have a view that uses a strongly typed ViewData similar to this:

namespace Site.web2.Models
{
    public class MySubData
    {
        public string Note { get; set; }
        public bool IsValid { get; set; }
    }
    public class MyViewData
    {
        public int DataId { get; set;}
        public List<MySubData> SubData { get; set; }

        public MyViewData()
        {
        }

        public void LoadDummyData()
        {
            DataId = 42;
            SubData = new List<MySubData>();
            SubData.Add(new MySubData() { Note = "Item 1" });
            SubData.Add(new MySubData() { Note = "Item 2" });
        }
    }
}

Controller:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Test1()
{
    Site.web2.Models.MyViewData data = new Site.web2.Models.MyViewData();
    data.LoadDummyData();
    return View(data);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Test1(Site.web2.Models.MyViewData data)
{
    return RedirectToAction("Index");
}

And the View is like this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Content/Site.Master"
     Inherits="System.Web.Mvc.ViewPage<Site.web2.Models.MyViewData>"
%>
<%= Html.BeginForm<HomeController>(c => c.Test1(null)) %>
<p>Id:</p>
<p><%= Html.TextBox("DataId")%></p>
<p>Note 1:</p>
<p><%= Html.TextBox("SubData[0].Note")%></p>
<p>Note 2:</p>
<p><%= Html.TextBox("SubData[1].Note")%></p>

<input type="submit" value="Submit" />
<% Html.EndForm(); %>

Ok, if I create a MyViewData, call LoadDummyData(), and use that in a View, I don't see my data in the TextBoxes.

The funny thing is, if I enter data in to the TextBoxes, that will get populated into the returned MyViewData in the Post controller.

What am I doing wrong? Was this fixed in a later version of MVC? I think I have RC 1.

Keith

Update 1

This

<%= Html.TextBox("DataId")%>

works just fine. I guess my question is, should this

<%= Html.TextBox("SubData[0].Note")%>

work the same way?

+1  A: 

Try this:

<p>Id:</p>
<p><%= Html.TextBox("DataId", Model.DataId)%></p>
<p>Note 1:</p>
<p><%= Html.TextBox("SubData[0].Note", Model.SubData[0].Note)%></p>
<p>Note 2:</p>
<p><%= Html.TextBox("SubData[1].Note", Model.SubData[1].Note)%></p>
Darin Dimitrov
The <%= Html.TextBox("DataId", Model.DataId) %> works just fine as <%= Html.TextBox("DataId") %>I was just expecting the <%= Html.TextBox("SubData[0].Note")%>to work the same way. I debugged into the MVC source and found the failure but I don't know .NET well enough to know if it should work.
Keith Murray