mercredi 15 avril 2015

MVC 5 Append Jquery Input form Bind as Object

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>

}

Aucun commentaire:

Enregistrer un commentaire