0

I want to create A form where related data from credit and debit is also added in create action of journal.

I have three tables with code.

public class Journal
{
    public int ID { get; set; }
    public DateTime journalDT { get; set; }
    public decimal Amount { get; set; }

    public virtual ICollection<Credit> Credits { get; set; }
    public virtual ICollection<Debit> Debits { get; set; }
}

public class Credit
{
    public int ID { get; set; }
    public int JournalID { get; set; }
    public int CodeID { get; set; }
    public decimal Amount { get; set; }

    public virtual Journal Journal { get; set; }
    public virtual Code Code { get; set; }
}

public class Debit
{
    public int ID { get; set; }
    public int JournalID { get; set; }
    public int CodeID { get; set; }
    public decimal Amount { get; set; }

    public virtual Journal Journal { get; set; }
    public virtual Code Code { get; set; }
}

Below is the form code

@using (Html.BeginForm())

{

Inputs for Jurnal Model......
<div class="row">
        <div class="col-sm-6 text-center" style="border:1px solid red;">
            Credit<br/>
            <input type="text" id="Credit[0].CodeID" name="Credit[0].CodeID" value=1 />
            <input type="text" id="Credit[0].Amount" name="Credit[0].Amount" value=23.00 />

            <input type="text" id="Credit[1].CodeID" name="Credit[1].CodeID" value=2 />
            <input type="text" id="Credit[1].Amount" name="Credit[1].Amount" value=123.00 />

            <input type="text" id="Credit[2].CodeID" name="Credit[2].CodeID" value=3 />
            <input type="text" id="Credit[2].Amount" name="Credit[2].Amount" value=223.00 />
        </div>
        <div class="col-sm-6 text-center" style="border:1px solid red;">
            Debit
            <input type="text" id="Debit[0].CodeID" name="Debit[0].CodeID" value=1 />
            <input type="text" id="Debit[0].Amount" name="Debit[0].Amount" value=23.00 />

            <input type="text" id="Debit[1].CodeID" name="Debit[1].CodeID" value=2 />
            <input type="text" id="Debit[1].Amount" name="Debit[1].Amount" value=123.00 />

            <input type="text" id="Debit[2].CodeID" name="Debit[2].CodeID" value=3 />
            <input type="text" id="Debit[2].Amount" name="Debit[2].Amount" value=223.00 />
        </div>
    </div>    

}

Now the problem is how can i bind the data in create action to Credits and Debits

public ActionResult Create([Journal journal, ICollection<Credit> Credits, Icollection<Debits> Debits)
  • 1
    Your properties are named `Credits` and `Debits` (plural), not `Credit` and `Debit` (so the controls need to be `name="Credits[0].CodeID"` etc. but do not do this. Use a `for` loop or custom `EditorTemplate` to generate the form controls - refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943). And your POST method only needs the `Journal journal` parameters –  Aug 02 '16 at 12:14
  • You wants to bind data from controller to view OR view to controller? – Sandip - Frontend Developer Aug 02 '16 at 12:14
  • Thanks Stephen, The pluralization worked. Initially there will be only one Credit and one Debit form, later the user will add other using java-script. – Javed Alam Aug 02 '16 at 16:53

0 Answers0