We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

CRUD with SQLEXPRESS EF returning null value from View to Controller

Hi Farveen,

The example you have sent me does not work please change it to read and write data to a MSSQL express database , that is what I am trying to do your example does not use EF and external sql database


Regards

Edmund Herbert

6 Replies

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team August 28, 2017 09:40 AM UTC

Hi Edmund,  
 
Thanks for contacting Syncfusion Support.  
 
We have prepared a sample in .Net Core and Entity FrameWork that can be downloaded from the following location.  
 
Sample:  
 
<ej-grid id="FlatGrid" allow-paging="true" > 
    <e-datamanager json="(IEnumerable<object>)ViewBag.datasource" insert-url="/Home/NormalInsert" adaptor="remoteSaveAdaptor" /> 
    <e-edit-settings allow-adding="true"/> 
    <e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string> { "add", "update", "cancel"})"/> 
    <e-columns> 
        <e-column field="OrderID" header-text="Order ID" text-align="Right" is-primary-key="true"></e-column> 
            . ..  
               . ..  
    </e-columns> 
</ej-grid> 
 
        public ActionResult NormalInsert([FromBody]CRUDModel<Orders> param) 
        { 
            _context.Add(param.Value); 
             _context.SaveChangesAsync(); 
            return Json(param.Value); 
        } 
        public IActionResult Index() 
        { 
            ViewBag.datasource = (IEnumerable)_context.Orders.ToList(); 
            return View(); 
        } 
 
Regards,  
Seeni Sakthi Kumar S. 



SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team August 28, 2017 09:44 AM UTC

Hi Edmund,  
 
Please ignore our previous response.  
 
We have prepared a sample in .Net Core and Entity FrameWork that can be downloaded from the following location.  
 
 
<ej-grid id="FlatGrid" allow-paging="true" > 
    <e-datamanager json="(IEnumerable<object>)ViewBag.datasource" insert-url="/Home/NormalInsert" adaptor="remoteSaveAdaptor" /> 
    <e-edit-settings allow-adding="true"/> 
    <e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string> { "add", "update", "cancel"})"/> 
    <e-columns> 
        <e-column field="OrderID" header-text="Order ID" text-align="Right" is-primary-key="true"></e-column> 
            . ..  
               . ..  
    </e-columns> 
</ej-grid> 
 
        public ActionResult NormalInsert([FromBody]CRUDModel<Orders> param) 
        { 
            _context.Add(param.Value); 
             _context.SaveChangesAsync(); 
            return Json(param.Value); 
        } 
        public IActionResult Index() 
        { 
            ViewBag.datasource = (IEnumerable)_context.Orders.ToList(); 
            return View(); 
        } 
 
Regards,  
Seeni Sakthi Kumar S. 



KS Kalai Sirajeddine January 2, 2018 09:41 AM UTC

Hi Seeni,

I've downloaded the sample and i wish u could send me the normalupdate and normaldelete source code for the controller as well as any other code needed to make it work.
Actually the insert is not adding in the database.

Regards,
Kalai Sirajeddine


SE Sathyanarayanamoorthy Eswararao Syncfusion Team January 3, 2018 05:22 AM UTC

Hi Kalai Sirajeddine, 
 
As per your requirement we have provided the working sample for update and delete method in the controller.  
Refer the code below. 

[Index.cshtml] 
            <ej-grid id="Grid" allow-paging="true" is-responsive="true" min-width="500"  > 
                <e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true" edit-mode="Normal"></e-edit-settings> 
                <e-toolbar-settings show-toolbar="true" toolbar-items='@new List<string> { "add", "edit","delete","update", "cancel" }'></e-toolbar-settings> 
                <e-datamanager adaptor="UrlAdaptor" insert-url="/Home/Insert" update-url="/Home/Update" remove-url="/Home/Delete" url="/Home/DataSource"></e-datamanager> 
                <e-columns> 
                    <e-column field="OrderID" is-primary-key="true" visible="false" is-identity="true"></e-column> 
                    <e-column field="EmployeeID" header-text="EmployeeID"></e-column> 
                    <e-column field="ShipCity" header-text="Ship City"></e-column> 
                 </e-columns> 
            </ej-grid> 
 
[HomeController] 
 
 
private NORTHWNDContext _context; 
        public HomeController(NORTHWNDContext context) 
        { 
            _context = context; 
        } 
 
        public IActionResult Index() 
        { 
             
            return View(); 
        } 
        public ActionResult DataSource([FromBody]DataManager dm) 
        { 
            IEnumerable data = _context.Orders.ToList(); 
            DataOperations operation = new DataOperations(); 
            if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting 
            { 
                data = operation.PerformSorting(data, dm.Sorted); 
            } 
            if (dm.Where != null && dm.Where.Count > 0) //Filtering 
            { 
                data = operation.PerformWhereFilter(data, dm.Where, dm.Where[0].Operator); 
            } 
            int count = data.Cast<Orders>().Count(); 
            if (dm.Skip != 0) 
            { 
                data = operation.PerformSkip(data, dm.Skip); 
            } 
            if (dm.Take != 0) 
            { 
                data = operation.PerformTake(data, dm.Take); 
            } 
            return Json(new { result = data, count = count }); 
 
        } 
        public ActionResult Insert([FromBody]CRUDModel<Orders> param) 
        { 
            _context.Orders.Add(param.Value);   //record to be added in DB 
            _context.SaveChanges(); 
            var data = _context.Orders.ToList(); 
            return Json(param.Value);     //return the added records 
        } 
        public ActionResult Update([FromBody]CRUDModel<Orders> param) 
        { 
            var db = _context; 
            db.Entry(param.Value).State = EntityState.Modified; 
            db.SaveChanges(); 
            return Json(param.Value); 
 
        } 
        public ActionResult Delete([FromBody]CRUDModel<Orders> param) 
        { 
            var db = _context; 
            Orders order = db.Orders.Where(c => c.OrderID == Convert.ToInt32(param.Key)).FirstOrDefault(); 
            db.Orders.Remove(order); 
            db.SaveChanges(); 
            return Json(order); 
        } 
 


We have prepared a sample for your reference which can be downloaded from your location. 

 
You have mentioned that insert is not adding the data in the database. Can you please provide more information about the error you are facing while inserting the record.  

Regards, 
Sathyanarayanamoorthy 



KS Kalai Sirajeddine January 3, 2018 10:23 AM UTC

Hi Sathyanarayanamoorthy,

Thanks for the sample its still working after removing :
 string connection = Configuration.GetConnectionString("NorthWndConnection");
           if (connection.Contains("%CONTENTROOTPATH%"))
            {
             connection = connection.Replace("%CONTENTROOTPATH%", _contentRootPath);
            }
and adding : var connection = @"Data Source = .\SQLEXPRESS01; Initial Catalog=NORTHWND; Integrated Security = True"; \\ my instance

Problem with insert was that the "OrderId" column was visible I had two sort of exeptions so when I put a value it says that Identity_Insert is off and when leave it empty I got Param is null, but all solved now thanks.

Still a question, I didn't get the role of ValuesController and if it can be just removed?

Regards,
Kalai Sirajeddine.



SE Sathyanarayanamoorthy Eswararao Syncfusion Team January 4, 2018 11:46 AM UTC

Hi Kalai Sirajeddine, 
 
We are happy to hear that your problem has been solved.  
 
And we are sorry for the Inconvenience caused. We have included the ValuesController by mistaken and it can be removed. 
 
Please let us know if you need further assistance. 
 
 
Regards, 
Sathyanarayanamoorthy 


Loader.
Live Chat Icon For mobile
Up arrow icon