MVC 2 model properties not used in the view are returned as null or empty

The problem I am facing is when I pass a populated object to a view that does not display all the properties.

public ActionResult Edit(Guid clientID)
{
    Property property = PropertyModel.GetPropertyDetails(clientID);
    return View("Edit", "Site", property);
}

View:

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<WebFrontend.ViewModels.Property>" %>

<% using (Html.BeginForm())
   {%>
    <%: Html.ValidationSummary(true, "Property update was unsuccessful. Please correct the errors and try again.") %>

    <fieldset>
        <legend>Edit Account: <%: Model.ClientAccountNo %></legend>

         <div class="editor-label">
            <%: Html.LabelFor(model => model.ClientName)%>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.ClientName)%>
            <%: Html.ValidationMessageFor(model => model.ClientName)%>
        </div>

        <p>
            <input type="submit" value="Update Property" />
        </p>
    </fieldset>

<% } %>

When submitted, the Property object is passed to this controller method, but all properties not used in the view are empty or empty, including Model.ClientAccountNo, which is present in the view before sending.

[HttpPost]
    public ActionResult Edit(Property property)
    {
        if (ModelState.IsValid)
        {
            bool success = PropertyModel.UpdateProperty(property);
            if (success)
            {
                // property has been updated, take them to the property details screen.
                return RedirectToAction("Details", "Property", new { clientID = property.ClientID });
            }
            else
            {
                ModelState.AddModelError("", "Could not update property.");
                return View("Activate", "Site", property);
            }
        }
        // If we got this far, something failed, redisplay form
        return View("Edit", "Site", property);
    }

I cannot find anything like this on the Internet, any explanation of why this is happening and how to fix it will be appreciated.

+3
source share
2 answers

MVC - , . . , .

, - .

, , - , .

+2

URL-, . Html.HiddenFor.

+1
source

All Articles