简体   繁体   中英

Object posted to edit method has null attributes from view ASP.NET MVC

I have a view with form that has data bind from model/DB with 1-to-many entites/tables.

The view references Contract class to form a contract object which has attributes: Customer ID FK, Customer virtual object, and other object's ID and virtual object related from other tables. I'm using Entity Framework.

Contract class from Model:

public partial class Contract
{
    public int Contract_ID { get; set; }
    public System.DateTime DateCreated { get; set; }
    public string CustomerId { get; set; }
    public Nullable<int> Status_ID { get; set; }
    public Nullable<int> SA_ID { get; set; }

    public virtual Customer Customer { get; set; }
    public virtual ContractStatus ContractStatus { get; set; }
    public virtual ServicesAccess ServicesAccess { get; set; }

}

I'm not using a View model, as it may introduce redundancy. Not sure how I can use a View Model too for this.

When I edit a contract via index page and save it posts a null Customer object from Contract to the HTTPPost edit method parameter. For a contract I only do not want the customer to be edited, so in the edit view I have:

@Html.HiddenFor(model => model.CustomerId)
<div class="display-field">
@Html.DisplayFor(model => model.Customer.CustomerLegalName)

So I get the customer id correctly from contract. Whereas the get Edit method does not have null Customer object. Why is this happening? This happens with ServicesAccess object too.

In the controller with edit method, to get the object to save I have to find the object by this way:

contract.ServicesAccess = db.ServicesAccesses.Find(contract.SA_ID);

If I don't do this then contract.Customer or contract.ServiceAccess is either null or initialized with null attributes. Cheers.

Because the default model binder in asp.net doesn't know how to bind to complex sub objects. You could write your own, but why would you need to. You are already passing the customer I'd back on the post.

You are trying to do to much in one model. The DefaultModelBinder will not bind complex properties of the Model. If you want to do what I think you are trying to achieve and post the Customer Object and have it tightly bound to a customer object in your controller then you need to create a partial view that takes type of Customer. In this partial view you will edit you customer and post to your controller with your customer properties that were on the view intact.

@model Contract

// some of your markup code here

// when you get to the Customer Edit do something like this
Html.Partial("EditForCustomerView", Model.Customer)

Then in that partial view put your customer properties.

When the page loads, does it have a value on model.Customer ? Did you try to step through it? I am guessing that there is a non-nullable value property inside the Customer object. If yes, try adding @Html.HiddenFor(model => model.Customer.NotNullableProperty) for that property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM