MVC Master Detail Saving Error
<p>Im trying to learn some MVC, therefore I created a webpage with a master/detail saving form. My problem is:
I get this error message: </p>

<pre><code>SERVER ERROR - The server encountered an unexpected condition that prevented it from fulfilling the request. (XHR): POST 
</code></pre>

<p>when I add <code>foreach()</code> in the controller action to save the details.
Without <code>foreach()</code> its working fine.</p>

<p>Im using a database first approach.</p>

<p>These are my tables:</p>

<pre><code>CREATE TABLE [dbo].[TicketMaster] (
[TicketID]            INT            IDENTITY (1, 1) NOT NULL,
[Titel]               VARCHAR (50)   NULL,
PRIMARY KEY CLUSTERED ([TicketID] ASC)
);
</code></pre>

<p>and</p>

<pre><code>CREATE TABLE [dbo].[TicketDetails] (
[TicketDetailsId] INT          IDENTITY (1, 1) NOT NULL,
[TicketID]        INT          NOT NULL,
[ItemName]        VARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([TicketDetailsId] ASC),
CONSTRAINT [FK_TicketDetails_TicketMaster] FOREIGN KEY ([TicketID])REFERENCES [dbo].[TicketMaster] ([TicketID])
);
</code></pre>

<p>My ViewModel:</p>

<pre><code>public class TLSModel 
{           
       public string Titel { get; set; }
       public List&lt;TicketDetail&gt; TicketDetails { get; set; }
}
</code></pre>

<p>Jquery:</p>

<pre><code>$(document).ready(function () {
  var list = [];
  $('#add').click(function () {
    list.push({
        ItemName: $('#ItemName).val().trim()          
    });
    $('#ItemName).val('').focus();
    GeneratedItemsTable();
});
$('#submit').click(function () {
    var data = {
        Titel: $('#Titel').val().trim(),            
        TicketDetails: list           
    }
    $.ajax({
        url: '/Ticket/Create',
        type: "POST",
        data: JSON.stringify( data ),
        dataType: "JSON",
        contentType: "application/json",
        success: function (d) {
            if (d.status == true) {
                alert('Successfully done.');
                list = [];
                $('#Titel').val('');
            }
            else {
                alert('Failed');
            }
            $('#submit').val('Save');
        },
        error: function () {
            alert('Error. Please try again.');
            $('#submit').val('Save');
        }
    });
});
function GeneratedItemsTable() {
 ...some Code...
}
});  
</code></pre>

<p>Controller Action:</p>

<pre><code>public JsonResult Create (TLSModel T)
    {
        bool status = true;            
        TicketMaster ticket = new TicketMaster { Titel = T.Titel };
        foreach(var i in T.TicketDetails)
        {
            ticket.TicketDetails.Add(i);
        }
        db.TicketMasters.Add(ticket);
        db.SaveChanges();
        return new JsonResult { Data = new { status = status } };            
    }    
</code></pre>