I have done a lot of research on this with only half success. My problem is this: I have a view in ASP.NET MVC with a View Model, the problem is that one of my ViewModel properties is a list of objects seen below:
public string ApplicationNo { get; set; }
public int HSCode { get; set; }
public IEnumerable<HSCode> HSCodes { get; set; }
public string ItemDescription { get; set; }
public int Weight { get; set; }
public QuantityUnit Quantity { get; set; }
public int Cost { get; set; }
public List<Item> Items { get; set; }
public class Item
{
public int HSCode { get; set; }
public string ItemDescription { get; set; }
public int Quantity { get; set; }
public int Weight { get; set; }
public double Cost { get; set; }
}
I am rendering new items to the user using a partial view and ajax. Partial View:
@model ecims.ViewModels.NewApplicationViewModel
<table id="myTableData" class="table" cellpadding="2">
@using (Html.BeginCollectionItem("newRow"))
{
if (Model.Items.Count != 0)
{
<tr>
<th>
@Html.DisplayNameFor(model => @Model.HSCode)
</th>
<td>
@Html.EditorFor(model => @Model.Items.FirstOrDefault().HSCode, new { htmlAttributes = new { @class = "form-control", @value = @Model.Items.FirstOrDefault().HSCode.ToString() } })
</td>
<th>
@Html.DisplayNameFor(model => @Model.Quantity)
</th>
<td>
@Html.EditorFor(model => @Model.Items.FirstOrDefault().Quantity, new { htmlAttributes = new { @class = "form-control", @value = @Model.Items.FirstOrDefault().Quantity.ToString() } })
</td>
<th>
@Html.DisplayNameFor(model => @Model.ItemDescription)
</th>
<td>
@Html.EditorFor(model => @Model.Items.FirstOrDefault().ItemDescription, "Item Description", new { htmlAttributes = new { @class = "form-control", @value = @Model.Items.FirstOrDefault().ItemDescription.ToString() } })
</td>
<th>
@Html.DisplayNameFor(model => @Model.Cost)
</th>
<td>
@Html.EditorFor(model => @Model.Items.FirstOrDefault().Cost, new { htmlAttributes = new { @class = "form-control", @value = @Model.Items.FirstOrDefault().Cost.ToString() } })
</td>
</tr>
}
else
{
<tr>
<th>
@Html.EditorFor(model => @Model.HSCode, new { htmlAttributes = new { @class = "form-control", @value = "" } })
</th>
<td>
@Html.EditorFor(model => @Model.HSCode, new { htmlAttributes = new { @class = "form-control", @value = "" } })
</td>
</tr>
}
}
</table>
My AJAX function is here:
<script type="text/javascript">
$("#add").on('click', function () {
var HSCode = $('#hsCode').val();
var ItemDescription = $('#descrip').val();
var Quantity = $('#qty').val();
var QuantityUnit = $('#qtyUnit').val();
var Cost = $('#cost').val();
$.ajax({
async: false,
data: { 'HSCode': HSCode, 'Quantity': Quantity, 'ItemDescription': ItemDescription, 'QuantityUnit': QuantityUnit, 'Cost': Cost },
url: '/CreateEUCApplication/CreateNewEUCItem',
success: function (partialView) {
$('#newRow').append(partialView);
}
});
});
</script>
I would like the 'Item' list in the ViewModel to be updated with new entries when the user clicks on the 'Add' button. Currently, my ajax method updates the view with new items successfully but when I post to the controller, the 'Model.Item' list is empty which throws up an exception.
How do I post a list in my ViewModel to my controller for processing? Thank you. PS: Any edits/suggestions to improve my code are more than welcome(I'm a novice trying to get better at this :-))