/Controller
//DAL
//MODEL has all the properties (Column1,2, etc)
//VIEW/AJAX
C#:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([DataSourceRequest] DataSourceRequest request, Model x)
{
if (x!= null && ModelState.IsValid)
{
DataLayer.Create();
}
return Json(new[] { x }.ToDataSourceResult(request, ModelState));
}
C#:
internal static Model Create()
{
int iRows = 0;
Guid guid = Guid.NewGuid(); (why is guid repeated twice?)
string query = @"INSERT INTO Table2" +
"([Column1], [Column2], [Column3], [Column4],
[Column5], [Column6], [Column7], [Column8],
[Column9], [Column10], [Column11], [Column12], [Column13] ) " +
" VALUES " + "(@Column1," + ConfigurationManager.AppSettings["Column2"] + ", ' ', GETDATE(), 0 ,@Column6,1,@Column8,@Column9,@BEGDATE, @END_DATE,@Column12, @Column13)";
using (IDbConnection _db = OpenConnection())
{
iRows = _db.Execute(query);
}
if (iRows > 0)
{
string query2 = @"SELECT * FROM Table2 WHERE PrimaryID2 = ";
using (IDbConnection _db = OpenConnection())
{
return _db.Query<Model>(query2, new { PrimaryID2 = guid.ToString() }).FirstOrDefault();
}
}
else
{
return null;
}
}
//VIEW/AJAX
HTML:
<div class="col">
@(Html.Kendo().Button()
.Name("create")
.Content("Add new row")
.HtmlAttributes(new { type = "button", = "btn btn-primary" })
.Events(ev => ev.Click("create")))
</div>
<div class="col">
.DropDownListFor(a => a.model.primarykey1, (IEnumerable<SelectListItem>)ViewBag.dropdownlist, "-- Select id1--", new { @class= "form-control", @id = "id1" })
</div>
</div>`
for my AJAX
function create(items) {
var selectedResident = $("#primaryid1").val();
$.ajax({
url: "/user/Create",
type: "POST",
data: { grid: items },
traditional: true, // add this
success: function (result) {
$('#grid').data('kendoGrid').dataSource.read();
$('#grid').data('kendoGrid').refresh();
},
error:
function (response) {
alert("Error: " + response);
}
});
}
Last edited by a moderator: