tags:

views:

46

answers:

1

Given the following code:

Models

class Log
{
   ...
   Ticket Ticket { get; set; }
   string Message { get; set; }
}
class Ticket
{
   ...
   Importance Importance { get; set; }
   string Name { get; set; }
   ...
}

View

<%@ Language="C#" Inherits="System.Web.Mvc.ViewPage<Models.Log>" %>
...
<%= Html.DisplayFor(l => l.Ticket.Name) %>
<%= Html.EditorFor(l => l.Message) %>
<%= Html.EditorFor(l => l.Ticket.Importance) %>
...

Controller Actions

[HttpGet]
public ActionResult Update(int id)
{
    Ticket t = _tickets.Get(id);
    return View(new Log { Ticket = t });
}

[HttpPost]
public ActionResult Update(Log l)
{
   // My problem is here:
    l.Ticket.Name; // This is null
    l.Ticket.Importance; // while this one is still set
}

Is there any way to persist the Ticket in the Log that is passed?

+3  A: 

Name will be output for display as plain text, meaning that there wont be a variable passed back to your page as part of your post as it isnt part of a form. Workarounds are to put it in a hidden field or lookup your model as part of your Update method then call UpdateModel on the retrieved item.

Wolfwyrd
I tested without the `<%= Html.DisplayFor(log => log.Ticket) %>` and it is still `null`.
Daniel A. White
You need to add hidden field, not to remove label. Try Html.Hidden(l => l.Ticket.Name) (syntax depend on MVC v2) or smth like <input type="hidden" name="Ticket.Hidden" value="<%= Model.Ticket.Name %>" />.
queen3