Question

I'm using the Entity Framework in my ASP MVC 3 site to handle a one-to-many relationship. The primary table (AgentTransmission) model object contains a List of secondary table objects (ClearninghouseParnters). Thanks to the EF, I don't need to do anything more than simply save the AgentTransmission object for the FK field of the ClearinghousePartners to populate and save.

However, the form the user gets has five fields for inputting Clearinghouse information like so.

enter image description here

Each row represents a new ClearinghousePartners list item object. Unfortunately each object is being saved to the database whether or not it contains data. This doesn't really make a difference performance-wise or even in how the page is displayed, however it will make for a pretty messy table.

My question is: Is there a way to instruct the Entity Framework to ONLY save items in the ClearinghousePartners list object if values exists in either the ClearinghouseName, TradingPartnersName, or StartDate column?

Likewise I would like to delete objects from the table if we find an item posted back to the controller with an existing ClearinghousePartners PK and no value/blanks in the same fields.

Primary Table Model

FYI - this table contains a LARGE number of fields so I'm just showing the List

public partial class AgentTransmission
{ 
    .
    .
    public virtual List<ClearinghousePartners> ClearinghousePartners { get; set; }
}

Secondary Table Model

public partial class ClearinghousePartners
{
    public int Id { get; set; }
    public string ClearingHouseName { get; set; }
    public string TradingPartnerName { get; set; }
    public Nullable<System.DateTime> StartDate { get; set; }
    public int AgtTransId { get; set; } //FK field corresponds to 'Id' on AgentTransmission

    public virtual AgentTransmission AgentTransmission { get; set; }
}

Controller

agenttransmission.LastChangeDate = DateTime.Now;
agenttransmission.LastChangeOperator = Security.GetUserName(User);
db.AgentTransmission.Add(agenttransmission);
db.SaveChanges(); //Saves to both tables

View

                <fieldset id="ClearinghousePartners">
                    <legend>Clearinghouse Partners</legend>
                    <center>
                        <table>
                            <thead>
                                <th>Clearinghouse Name</th>
                                <th>Trading Partner Name</th>
                                <th>Start Date</th>                             
                            </thead>
                            <tbody>
                                @for (int i = 0; i < Model.ClearinghousePartners.Count(); i++)
                                {
                                    <tr align="center">
                                        @Html.HiddenFor(model => model.ClearinghousePartners[i].Id)
                                        @Html.HiddenFor(model => model.ClearinghousePartners[i].AgtTransId)
                                        <td>@Html.TextBoxFor(model => model.ClearinghousePartners[i].ClearingHouseName, new { style = "width: 100px" })</td>
                                        <td>@Html.TextBoxFor(model => model.ClearinghousePartners[i].TradingPartnerName, new { style = "width: 100px" })</td>
                                        <td>@Html.TextBoxFor(model => model.ClearinghousePartners[i].StartDate, new { style = "width: 100px" })</td>                                    
                                    </tr>
                                }
                            </tbody>
                        </table>                            
                    </center>
                </fieldset>
Was it helpful?

Solution

The easiest way is to work out some condition that qualifies the object as "non-null", i.e. the user entered something for it, so it should be saved. Then, remove any items from the list on POST that are "null".

agenttransmission.ClearingHousePartners
    .Where(m => string.IsNullOrWhitespace(m.ClearingHouseName))
    .Remove();

That will remove any items from the list if the posted name value is nothing. If you want additional qualifications to determine that the object shouldn't be saved, just add them as additional clauses in the Where.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top