volosoft / jtable

A JQuery plugin to create AJAX based CRUD tables.
http://www.jtable.org
1.1k stars 506 forks source link

Dropdown value cannot view in "Add/Edit record form" box using C#.net #2262

Open AhmadYusoff opened 3 years ago

AhmadYusoff commented 3 years ago

Hello I am new to jtable and like to improve programming skill but i am stuck on dropdown function and unable to display inside ADD/EDIT form . It come out weird value of dropdown is "undefined" . If i manual hardcore for options of Juice name will not issue and working fine.

Below is my code:

In controller part:

public JsonResult GetJuiceLists() { try { SelectList listJuice; var sql = "SELECT JUICE_ID,JUICE_NAME FROM JUICE"; listJuice = new SelectList(SqlDbContext.getDropdownList(sql), "Value", "DisplayText"); return Json(new { Result = "OK", Options = listJuice}, JsonRequestBehavior.AllowGet); } catch (Exception ex) { return Json(new { Result = "ERROR", Message = ex.Message }); } }

public List<ViewModels.Dropdown_VM> getDropdownList(string sql)

{ try {

            var list = new List<ViewModels.Dropdown_VM>();
            ConnOpen();
            SqlCommand SQLcmd = new SqlCommand(sql, _SQLconn);
            SQLcmd.CommandTimeout = 30;
            SQLcmd.CommandText = sql;
            SQLcmd.Connection = _SQLconn;

            SqlDataReader dr = SQLcmd.ExecuteReader();
            while (dr.Read())
            {
                list.Add(new ViewModels.Dropdown_VM
                {
                    Value = dr.GetInt32(0),
                    DisplayText = dr.GetString(1)
                }
                    );
            };
            return list;

        }
        catch (Exception exc)
        {
            throw exc;
        }
        finally
        {
            ConnClose();
        }
    }

In javascript part :

fields:{ .... JUICE_ID: { list: false, title: 'Juice', width: '12%', options: '/Fruit/GetJuiceLists' //options: { // '1': 'APPLE', '2': 'BANANA', '3': 'CHERRIES', '4': 'DRAGON FRUIT', '5': 'MANGO', '6': 'PINEAPPLE', // '7': 'GUAVA', '8': 'HONEYDEW MELON', '9': 'CHERRIES', '10': 'WATERMELON', '11': 'KIWI', '12': 'OTHERS' // } }, ..... }

Can someone has any experience with that issue? Thank you so much.

misterparsons commented 3 years ago

The jtable definition looks fine, so the problem must be with the json response from the server.

Can you post the server response here for further investigation.

AhmadYusoff commented 3 years ago

json response from server coding on below:

public JsonResult GetJuiceLists() { try { SelectList listJuice; var sql = "SELECT JUICE_ID,JUICE_NAME FROM JUICE"; listJuice = new SelectList(SqlDbContext.getDropdownList(sql), "Value", "DisplayText"); return Json(new { Result = "OK", Options = listJuice}, JsonRequestBehavior.AllowGet); } catch (Exception ex) { return Json(new { Result = "ERROR", Message = ex.Message }); } }

misterparsons commented 3 years ago

I meant, what is the json text arriving at the browser?

I'd expect to see something like

{"Result":"OK","Options":[{"Value":"101","DisplayText":" Paperback"},{"Value":"102","DisplayText":" Hardcover"}]}
dasguptahirak commented 3 years ago

@AhmadYusoff You can view the ajax response of '/Fruit/GetJuiceLists' in Network tab under "Developer Tools". Share that response