tags:

views:

50

answers:

1

Hi I have the following view that gets the values for the form fields from a viewdata that contains an instance of a class ApplicationSettingEntity. the ApplicationSettingEntity class contains an instance of a machineentity class as one of the attributes. The form is populated by values from item.Id item.Key item.Machine.Name

When I edit the values the form returns the updated values for item.Id and item.Key but not item.Machine.Name

I inspected the value of item.Machine.Name as I am hydrating the form and It is setting the value of the text box correctly but data posted back for item.Machine is null when I hit the save button. What am I doing wrong?

        <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

        <%@ Import Namespace="AppSettings.Web.Models" %>
        <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
            Edit Application Setting
        </asp:Content>
        <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
            <strong style="color: Black;">
            <div>
                <%  if (ViewData["ApplicationSetting"] != null)
                    {
                        var item = (ApplicationSettingEntity)ViewData["ApplicationSetting"];
                %>
                        <%=Html.ActionLink("Back to List", "Index",
                                                       new
                                                           {
                                                               controller = "ApplicationSettings",
                                                               Applications = item.Id,
                                                               Machines = item.Machine.Id
                                                           })%>
                        </div>
                        <%=Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.")%>
                        <%
                            using (Html.BeginForm((bool)ViewData["IsAdd"] ? "SaveAdd" : "SaveEdit", "ApplicationSettings"))
                            {%>        
                                <fieldset>
                                    <legend><strong>Application Name:</strong>
                                    </legend>
                                    <p>
                                        <label for="Id" style="font-weight: bold;">
                                            Application Setting Id:</label>
                                        <%=Html.TextBox("Id", item.Id)%>
                                        <%=Html.ValidationMessage("Id", "*")%>
                                    </p>
                                    <p>
                                        <label for="Key" style="font-weight: bold;">
                                            Application Setting Key:</label>
                                        <%=Html.TextBox("Key", item.Key, new {style = "width:400px;"})%>
                                        <%=Html.ValidationMessage("Key", "*")%>
                                    </p>
                                    <p>
                                        <span style="font-weight: bold;">Machine Name: </span>            
                                        <%=Html.TextBox("MachineName", item.Machine.Name)%>

                                    </p>
                                    <p>
                                        <input type="submit" value="Save" />
                                    </p>
                                </fieldset>
                                <%
                            }%>
                        <div>
                            <%=Html.ActionLink("Back to List", "Index",
                                                           new
                                                               {
                                                                   controller = "ApplicationSettings",
                                                                   applicationId = item.Application.Id,
                                                                   machineId = item.Machine.Id
                                                               })%>
                        </div>
                        <%
                }%>
        </asp:Content>
+3  A: 

You get the value of the post in the item MachineName of the form collection, because that is the first param of Html.TextBox

Eduardo Molteni