tags:

views:

25

answers:

1

I have a Edit View generated by VS 2010 (code below). I don't want the user to see the ListID, so i commented it out.

What happens is that if I comment the <%: Html.TextBoxFor(model => model.ListID) %> line, when I hit submit, the Controller instead of receiving something like a DSList with name="x" and listID=10, always gets the listID=0.

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

    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        Edit
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <h2>
            Edit</h2>
        <% using (Html.BeginForm())
           {%>
        <%: Html.ValidationSummary(true) %>
        <fieldset>
            <legend>Fields</legend>

            <div class="editor-field">
                <%--<%: Html.TextBoxFor(model => model.ListID) %>--%>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Name) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Name) %>
                <%: Html.ValidationMessageFor(model => model.Name) %>
            </div>
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
        <% } %>
        <div>
            <%: Html.ActionLink("Back to List", "Index") %>
        </div>
    </asp:Content>

Controller:

[HttpPost]
        public ActionResult Edit(DSList l)
        {
 // l always comes with the id equal to 0.
... logic ...
}

What can I do to receive the correct object?

Thank you

+3  A: 

You need to include the ID in a hidden field.

SLaks
Thank you SLaks! Is there any way I can do this without the hidden field? I was looking at a project without the hidden field, but I can't understand how the value is passed.
Artur Carvalho
You could put the ID in the query string, or parse it manually from a cookie. However, you cannot magically persist state without putting it anywhere.
SLaks