0

I have tried the Append and Bind solution in here. This solution

But Still having problem.

I have two Models. 1st is Pricing

public class Pricing
{
    public int PricingID { get; set; }

    public int PricePerSack { get; set; }
    public virtual ExpensePersack ExpensePerSacks { get; set; }

    public string companyName { get; set; }
    public string BankName { get; set; }
    public string Branch { get; set; }
    public Terms Term { get; set; }
    public int ChequeNumber { get; set; }
    public DateTime? DateofCheque { get; set; }


}
   public enum Terms { Cash,Cheque};

}

Next is ExpensePerSack Model.

public class ExpensePersack
    {
        public int ExpensePersackID { get; set; }
        public int PricingID { get; set; }

        public string Description { get; set; }
        public int Amount { get; set; }

}

Controller

public ActionResult Create([Bind(Include = "PricingID,PricePerSack,companyName,BankName,Branch,Term,ChequeNumber,DateofCheque")] Pricing pricing, ExpensePersack ExpensePersack)
    {
        if (ModelState.IsValid)
        {
            db.Pricings.Add(pricing);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(pricing);
    }

Now In the View. This is Clipped Having hard time editing this Question Because of the codes getting broken when copy paste.

Now in the Controller I haven't added the Expense model to be saved or the code for it, because in the Post Method. The Expense is null from the Controller.

I still don't know how I can Send the Data to the controller or even Delete Appended column by their IDs.

 @model RMQGrains.Models.Pricing


    ... bunch of codes from Pricing Models editor for...

    <div id="InsertView"><div>



    <input type="button" id="addrow" value="Add">

<div id="NewBatchProduct" style="display:none">
    <tr>
        <td><input type="text" name="ExpensePricing[#].Description" value />   </td>
     <td><input type="text" name="ExpensePricing[#].Amount" value /></td>
      <td>
         <input type="hidden" name="ExpensePricing.Index" value="%" />
         <a class="deleteRow">Delete</a>
        </td>
    </tr>
</div>



    @section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

      <script>
       $("#addrow").click(function () {
           var index = (new Date()).getTime(); // unique indexer
           var clone = $('#NewBatchProduct').clone(); // clone the       BatchProducts item
           // Update the index of the clone
           clone.html($(clone).html().replace(/\[#\]/g, '[' + index + ']'));
           clone.html($(clone).html().replace(/"%"/g, '"' + index + '"'));
           $("#InsertView").append(clone.html());
       });
   </script>

   }
Community
  • 1
  • 1
Aizen
  • 1,705
  • 2
  • 12
  • 25
  • include some code or else this is too broad for a decent answer. – DLeh Apr 14 '15 at 14:52
  • Some options for dynamically adding items [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) –  Apr 14 '15 at 22:58
  • @Stephen I have tried @for(int i = 0; i < Model.BatchProducts.Count; i++) { @Html.TextBoxFor(m => m.BatchProducts[i].Quantity) @Html.TextBoxFor(m => m.BatchProducts[i].BatchName) // add the following to allow for dynamically deleting items in the view Delete } As you suggested, I added Delete inside the tag. How am I going to delete call on this? – Aizen Apr 15 '15 at 07:15
  • @Aizen, You need to edit you question with the new code - too hard to read in the comments –  Apr 15 '15 at 07:17
  • Thanks, I will construct everything in my Question. – Aizen Apr 15 '15 at 07:31
  • Took me 35 mins to Edit. Lol – Aizen Apr 15 '15 at 08:08
  • You edit makes no sense at all. You don't even have a property which is a collection. Why do you have parameter `ExpensePersack ExpensePersack` in the POST method? Why are you not just generating controls for the `ExpensePersack` property? The script you have taken from my link generates controls for a collection property named `ExpensePricing` which does not exist! What are you actually trying to do here? –  Apr 15 '15 at 08:52
  • Yep, I saw that. Sorry about the mistake on the naming. I have changed ExpensePricing with ExpensePersack. The Controller is now getting the information. With an [] – Aizen Apr 15 '15 at 09:05
  • Thank you for the suggestion. I will try to find a tutorial about List in the models. The reason I am not making an Individual ExpensePersack controller is, My Expense persack cannot be Generated on its own. It needs to be edited in the pricing But it has its own table. I need to make a Expensepersack data with the pricing ID when it is generated. Meaning I will generate both of them at the same time in the database. – Aizen Apr 15 '15 at 09:25
  • @Aizen, I'm confused with what your trying to do. Property `ExpensePerSacks` is an object in your model (not a collection) yet the script is generating a collection of `ExpensePerSacks` objects. –  Apr 15 '15 at 09:29
  • I have a Pricing Table and a Expense Sack table. -> I am not really sure how I will construct my Models relation about this. If I will create a pricing data, then Create an Expense data that will have a key to my Pricing ID. That is fine. But the problem is, In my view I have to implement on Creating both at the same Time and I am not even sure if it is possible since, If I am going to take the Pricing ID inside my Expense data. I could not cause both needs to generate at the same time. So if there is another approach that I can take, That I can use appending the Input. – Aizen Apr 15 '15 at 09:42
  • So in my Pricing Create View. I have Pricing and Expense at the same time. While the Expense Input is Dynamic. And making both of them relate to each other. Again, not sure how my models needs to be edited about this. – Aizen Apr 15 '15 at 09:44
  • @Aizen, If you want me to get a notification, look at how I start this message. There is no reason to use any scripts at all. You just need to generate controls for `ExpensePersack` - @Html.TextBoxFor(m => m.ExpensePersack.Description)` etc. When you post, Save the `Pricing` onject, then gets its `ID` property, then you can save the `ExpensePersack` object. But start by creating view models that include only the properties that you need to edit in the view. –  Apr 15 '15 at 10:03

0 Answers0