Dear Friends,
I have designed inventory module with textbox and web grid. i need add items from textbox to webgrid dynamically.. Please help me..
What I have tried:
View:
@if (Model != null && Model.Count > 0)
{
int j = 0;
foreach (var i in Model)
{
j++;
}
}
Item Name | Item Rate Code | Item Code | Qty Requested | |
@Html.TextBox("ItemName", null, new { id = "ItemName", @class = "form-control", autocomplete = "off" }) | @Html.TextBoxFor(a => a[j].IRateCode, new { id = "RateCode", @class = "form-control", autocomplete = "off" }) | @Html.TextBoxFor(a => a[j].ItemCode, new { id = "ItemCode", @class = "form-control", autocomplete = "off" }) | @Html.TextBoxFor(a => a[j].QtyRequested, new { id = "QtyRequested", @class = "form-control", autocomplete = "off" }) | @if (j > 0)
{
Remove
}
|
<script language="javascript">
$(document).ready(function () {
//1. Add new row
$("#addNew").click(function (e) {
e.preventDefault();
var $tableBody = $("#dataTable");
var $trLast = $tableBody.find("tr:last");
var $trNew = $trLast.clone();
var suffix = $trNew.find(':input:first').attr('name').match(/\d+/);
$trNew.find("td:last").html('
Remove');
$.each($trNew.find(':input'), function (i, val) {
// Replaced Name
var oldN = $(this).attr('name');
var newN = oldN.replace('[' + suffix + ']', '[' + (parseInt(suffix) + 1) + ']');
$(this).attr('name', newN);
//Replaced value
var type = $(this).attr('type');
if (type.toLowerCase() == "text") {
$(this).attr('value', '');
}
// If you have another Type then replace with default value
$(this).removeClass("input-validation-error");
});
$trLast.after($trNew);
// Re-assign Validation
var form = $("form")
.removeData("validator")
.removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse(form);
});
// 2. Remove
$('a.remove').live("click", function (e) {
e.preventDefault();
$(this).parent().parent().remove();
});
});
</script>
@* This lines for multi line insert temprary*@
@System.Web.Optimization.Scripts.Render("~/bundles/jquery")