WillMcKill
WillMcKill

Reputation: 390

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

The issue I am having is when i pass a populated object to a view that doesn't display all of 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 of the properties not used in the view are null or empty including Model.ClientAccountNo which is present on the view before submitting.

[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 can't find anything similar on the web, any explanation for why this is happening and how to fix it would be appreciated.

Upvotes: 3

Views: 3009

Answers (2)

Rob West
Rob West

Reputation: 5241

This is the way MVC works - it tries to construct an object of the action parameter type from the values in the route, form and query string. In your case it can only get the values in the form collection. It does not know anything about the values that you have stored in your database.

If you are only interested in certain properties you would be better off doing a specific view model with just these on - then you can validate for this specific case.

If you store values in hidden fields keep in mind that these can be manipulated - if this is a problem definitely go with a specific view model with only the editable fields on.

Upvotes: 2

Jakub Konecki
Jakub Konecki

Reputation: 46008

Since you are not passing unused properties in URL, then you will have to render hidden fields for them. Use Html.HiddenFor method.

Upvotes: 0

Related Questions