在编辑时,表单不会返回所有更新的值

时间:2009-09-08 21:25:16

标签: c# asp.net-mvc

您好我有以下视图,它从包含类ApplicationSettingEntity的实例的viewdata获取表单字段的值。 ApplicationSettingEntity类包含一个机器实体类的实例作为其中一个属性。表单由来自的值填充 item.Id item.Key item.Machine.Name

当我编辑值时,表单返回item.Id和item.Key的更新值,但不返回item.Machine.Name

我检查了item.Machine.Name的值,因为我正在为表单保湿,它正确设置了文本框的值,但是当我点击保存按钮时,为item.Machine发回的数据为空。我究竟做错了什么?

        <%@ 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>

1 个答案:

答案 0 :(得分:3)

您可以在表单集的MachineName项中获取帖子的值,因为这是Html.TextBox

的第一个参数
相关问题