Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASP.NET Core MVC + IIS Empty Error Message on Listing #2198

Closed
vector-man opened this issue Nov 23, 2018 · 11 comments
Closed

ASP.NET Core MVC + IIS Empty Error Message on Listing #2198

vector-man opened this issue Nov 23, 2018 · 11 comments

Comments

@vector-man
Copy link

Hello. I am having difficulty getting a basic table to list data with ASP.NET Core MVC. Everything I've tried results in an empty error message on IIS. Using Postgresql + Code first.

Model

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting.Internal;

namespace MyProject.Models
{
    public class User : IEntity
    {
        [Key]
        public long Id { get; set; }

        [Required]
        public string FirstName { get; set; }

        [Required]
        public string LastName { get; set; }

        [Required] public DateTime CreationTime { get; set; } = DateTime.Now;

        [Required]
        [MinLength(12)]
        public string Password { get; set; }

        [Required]
        [EmailAddress]
        public string Email { get; set; }

        [Required]
        public string Address1 { get; set; }

        public string Address2 { get; set; }

        [Required]
        public string City { get; set; }

        public string Country { get; set; }

        [Required]
        [StringLength(10, MinimumLength = 5)]
        [RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Postal Code")]
        public string PostalCode { get; set; }

        [Required]
        public string State { get; set; }

        [Required]
        [Phone]
        public string Phone { get; set; }
    }
}

In the View:

@{
    ViewData["Title"] = "Admin";
}

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
        integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
        crossorigin="anonymous"></script>
<!-- jTable style file -->
<link href="lib/jtable.2.4.0/themes/metro/darkorange/jtable.min.css" rel="stylesheet" type="text/css" />

<!-- A helper library for JSON serialization -->
<script type="text/javascript" src="lib/jtable.2.4.0/external/json2.min.js"></script>
<!-- Core jTable script file -->
<script type="text/javascript" src="lib/jtable.2.4.0/jquery.jtable.min.js"></script>
<!-- ASP.NET Web Forms extension for jTable -->
<script type="text/javascript">
    $(document).ready(function () {
        $('#content').jtable({
            title: 'Users',
            paging: true,
            pageSize: 10,
            sorting: false,
            actions:
            {
                listAction: '@Url.Action("GetUsers")'

            },
            fields: {

                FirstName: {
                    key: false,
                    title: 'FirstName',
                    width: '40%'
                }
            }
        });

        $('#content').jtable('load');
    });
</script>
<div id="content"></div>


Controller::

namespace MyProject.Controllers.Admin
{
    public class AdminController : Controller
    {
        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult GetUsers(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            var userCount = GetUserCount();
            var user = GetUsersList(jtStartIndex, jtPageSize, jtSorting);
            return Json(new { Result = "OK", Records = user, TotalRecordCount = userCount });
        }

        public int GetUserCount()
        {
            // Instance of DatabaseContext
            //Customers is DbSet and Table name in Database is Customers
            using (var context = new UsersContext())
            {
                return context.Users.Count();
            }
        }

        public List<User> GetUsersList(int startIndex, int count, string sorting)
        {
            // Instance of DatabaseContext
            using (var context = new UsersContext())
            {
                IEnumerable<User> query = context.Users;

                //Sorting Ascending and Descending
                if (string.IsNullOrEmpty(sorting) || sorting.Equals("Address1 ASC"))
                {
                    query = query.OrderBy(p => p.Address1);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("Address1 DESC"))
                {
                    query = query.OrderByDescending(p => p.Address1);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("Address2 ASC"))
                {
                    query = query.OrderBy(p => p.Address2);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("Address2 DESC"))
                {
                    query = query.OrderByDescending(p => p.Address2);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("City ASC"))
                {
                    query = query.OrderBy(p => p.City);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("City DESC"))
                {
                    query = query.OrderByDescending(p => p.City);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("Country ASC"))
                {
                    query = query.OrderBy(p => p.Country);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("Country DESC"))
                {
                    query = query.OrderByDescending(p => p.Country);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("CreationTime ASC"))
                {
                    query = query.OrderBy(p => p.CreationTime);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("CreationTime DESC"))
                {
                    query = query.OrderByDescending(p => p.CreationTime);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("Email ASC"))
                {
                    query = query.OrderBy(p => p.Email);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("Email DESC"))
                {
                    query = query.OrderByDescending(p => p.Email);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("FirstName ASC"))
                {
                    query = query.OrderBy(p => p.FirstName);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("FirstName DESC"))
                {
                    query = query.OrderByDescending(p => p.FirstName);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("LastName ASC"))
                {
                    query = query.OrderBy(p => p.LastName);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("LastName DESC"))
                {
                    query = query.OrderByDescending(p => p.LastName);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("PostalCode ASC"))
                {
                    query = query.OrderBy(p => p.PostalCode);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("PostalCode DESC"))
                {
                    query = query.OrderByDescending(p => p.PostalCode);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("State ASC"))
                {
                    query = query.OrderBy(p => p.State);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("State DESC"))
                {
                    query = query.OrderByDescending(p => p.State);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("Country ASC"))
                {
                    query = query.OrderBy(p => p.Country);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("Country DESC"))
                {
                    query = query.OrderByDescending(p => p.Country);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("Phone ASC"))
                {
                    query = query.OrderBy(p => p.Phone);
                }
                else if (string.IsNullOrEmpty(sorting) || sorting.Equals("Phone DESC"))
                {
                    query = query.OrderByDescending(p => p.Phone);
                }
                else
                {
                    query = query.OrderBy(p => p.Id); //Default!
                }

                return count > 0
                           ? query.Skip(startIndex).Take(count).ToList()  //Paging
                           : query.ToList(); //No paging
            }
        }
    }
}
@rahsharm77
Copy link

Is there any update on this issue? I am also facing the same issue in .NET Core and still have not found a solution to it. Thanks

@maliming
Copy link
Member

@rahsharm77
Can you talk about the specific situation?
If there is an exception, please share the exception details.

@rahsharm77
Copy link

rahsharm77 commented Mar 28, 2019

@maliming
There is no exception, no error message. Just a blank ERROR dialog box opens up and there is not StackTrace also. I tried running the same project in a .NET MVC and there it is working fine but not in .NET Core.

@maliming
Copy link
Member

Can you share a project recurring problem? So I can download the project and try it out.

@rahsharm77
Copy link

@maliming

Okay, I will do that for you and let you know. Thanks for your assistance in this.

@rahsharm77
Copy link

@maliming I have setup two repositories for you:

  1. Is for JTable Example run in .NET MVC 4.5: https://github.com/rahsharm77/JTableExample-.NET-MVC

  2. Is for JTable Example run in .NET CORE 2.1: https://github.com/rahsharm77/JTableExample-.NET-Core

Note that the Model, View and Controller code is the same in both the projects. The plugin is running fine in .NET MVC but if you run it in .NET CORE, a empty alert box is displayed without any error.

Please let me know if you find a way to resolve this issue.

Thank You

@maliming
Copy link
Member

@rahsharm77 I will try it out.

@maliming
Copy link
Member

@rahsharm77
This is because the naming style used by jtable is not the same as the aspnet core.

Solution:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1).AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

@rahsharm77
Copy link

rahsharm77 commented Mar 28, 2019

@maliming It works like a charm. Thank you very much for your valuable input on this problem. If you could elaborate on what was the problem initially with this plugin compatibility with .NET CORE, that would be great to understand.

@maliming
Copy link
Member

Mainly because the naming used by jtable is inconsistent with the json returned by aspnet core.
https://github.com/volosoft/jtable/blob/master/dev/jquery.jtable.core.js#L420

{
    "Result": "OK"
}
{
    "result": "OK"
}

@rahsharm77
Copy link

@maliming Makes sense. Thank you so much for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants