How to select user from kendo dropdown then click kendo button do add row to Database table and displayed with kendo grid passing JSON to ajax functio

ah10777

New member
Joined
Oct 9, 2024
Messages
2
Programming Experience
Beginner
/Controller
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));

}
//DAL
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;

    }

}
//MODEL has all the properties (Column1,2, etc)

//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:
I've moved your thread to a more general subforum since this is an issue of working with a specific 3rd party library for web. I also put your code in code tags. Please put your code in code tags in the future.
 
I'm general you shouldn't be driving the UI to perform data changes, specially if you have access to the data. You should just update the underlying data, and then refresh the UI to reflect the changes.
 
I'm general you shouldn't be driving the UI to perform data changes, specially if you have access to the data. You should just update the underlying data, and then refresh the UI to reflect the changes.

I am pretty new to this, can you explain it a bit more? how would I not do it from the UI?
 
Back
Top Bottom