locked
Debug Server Responded with a status 500 RRS feed

  • Question

  • User-1140746197 posted

    Hi,

    I have a grid table to show all the OptMsgType Record in OptMsgType table by JSON. I can load and delete the table but if I click the "Create" button, it will pop up a blank page ("Create.cshtml").  I check the browser and it shows the error "Failed to load resource: the server responded with a status of 500 ()" in "Settings/OptMsgType/Create:1".  I googled it and it seems kind of server error. I would like to ask if I have way to debug the cause of error.

    Thank you!

    Create.cshtml

    @model theManager.Areas.Settings.Models.OptMsgType
    
    
    @{
        ViewData["Title"] = "Create";
        Layout = null;
    }
    
    <h2>Message Type Settings</h2>
    
    
    @using (Html.BeginForm("Create","OptMsgType", FormMethod.Post, new { id= "popupForm" }))
    {
        if (Model != null && Model.MsgTypeId > 0)
        {
            @Html.HiddenFor(a=>a.MsgTypeId)
        }
     
        <div class="form-group">
            <label>Message Type ID</label>
            @Html.TextBoxFor(a=>a.MsgTypeId,new { @class = "form-control" })
            @Html.ValidationMessageFor(a=>a.MsgTypeId)
        </div>
        <div class="form-group">
            <label>Leave Type</label>
            @Html.TextBoxFor(a => a.MsgType, new { @class = "form-control" })
            @Html.ValidationMessageFor(a => a.MsgType)
        </div>
        <div class="form-group">
            <label>Status</label>
            @Html.DropDownListFor(model => model.MsgStatus, new SelectList(Model.StatusList, "Value", "Text"), htmlAttributes: new { @class = "form-control", id = "OptStatus" })
            @Html.ValidationMessageFor(a => a.StatusList)
        </div>
        
        <div>
            <input type="submit" value="Create" />
        </div>
    }
     

    OptMsgType.cs

    namespace theManager.Areas.Settings.Models
    {
            public class OptMsgType
            {
                [Required]
                [Key]
                public int MsgTypeId { get; set; }
                [Required]
                public string MsgType { get; set; }
                [Required]
                public string MsgStatus { get; set; }     
                [NotMapped]
                public IEnumerable<SelectListItem> StatusList { get; set; }
            }
    }
    

    OptMsgTypeController.cs

    namespace theManager.Areas.Settings.Controllers
    {
        [Area("Settings")]
        public class OptMsgTypeController : Controller
        {
            private readonly theManagerContext _context;
    
            public OptMsgTypeController(theManagerContext context)
            {
                _context = context;
            }
    
        
            public IActionResult Index()
            {
                return View();
            }
    
            public IActionResult GetMsgTypes()
            {
                var msgTypes = _context.OptMsgType.OrderBy(a => a.MsgTypeId).ToList();
                return Json(new { data = msgTypes });
            }
    
            [HttpGet]
            public ActionResult Create()
            {
                var MsgType = new OptMsgType()
                {
                    StatusList = _context.OptStatus.Select(c => new SelectListItem
                    {
                        Value = c.StatusId.ToString(),
                        Text = c.StatusName
                    })
                };
            
                return View("Create", MsgType);
                    
            }
            
            [HttpGet]
            public IActionResult Create(int id)
            {
                var v = _context.OptMsgType.Where(a => a.MsgTypeId == id).FirstOrDefault();
                return View(v);
    
            }
    
            [HttpPost]
            public IActionResult Create(OptMsgType newMsgType)
            {
                bool status = false;
                
                var ExistId = _context.OptMsgType.FirstOrDefault(w => w.MsgTypeId == newMsgType.MsgTypeId);
    
    
                if (ModelState.IsValid)
                {
    
                    if (ExistId!=null)
                    {
                        
                        //Edit 
                        var v = _context.OptMsgType.Where(a => a.MsgTypeId == newMsgType.MsgTypeId).FirstOrDefault();
                        if (v != null)
                        {
                            v.MsgTypeId = newMsgType.MsgTypeId;
                            v.MsgType = newMsgType.MsgType;
                            v.MsgStatus = newMsgType.MsgStatus;
                        }
                    }
                    else
                    {
                        //Save
                        
                        _context.OptMsgType.Add(newMsgType);
                    }
                    _context.SaveChanges();
                    status = true;
                }
    
                return new JsonResult(new { status = status });
            }
    
    
            [HttpGet]
            public IActionResult Delete(int id)
            {
    
                var v = _context.OptMsgType.Where(a => a.MsgTypeId == id).FirstOrDefault();
                if (v != null)
                {
                    return View(v);
                }
                else
                {
                    return NotFound();
                }
    
            }
    
            [HttpPost]
            [ActionName("Delete")]
            public IActionResult DeleteMsgType(int id)
            {
                bool status = false;
                var v = _context.OptMsgType.Where(a => a.MsgTypeId == id).FirstOrDefault();
                if (v != null)
                {
                    _context.OptMsgType.Remove(v);
                    _context.SaveChanges();
                    status = true;
                }
    
    
                return new JsonResult(new { status = status });
            }
        }
    }

    Index.cshtml

    @{
        ViewData["Title"] = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <div style="width:90%; margin:0 auto" class="tablecontainer">
        <a class="popup btn btn-primary" href="/Settings/OptMsgType/Create" style="margin-bottom:20px; margin-top:20px;">Add New Msg Type</a>
        <table id="OptMsgTypeTable">
            <thead>
                <tr>
                    <th>Message Type ID</th>
                    <th>Message Type</th>
                    <th>Status</th>
                    <th>Edit</th>
                    <th>Delete</th>
                </tr>
            </thead>
        </table>
    </div>
    
    @section scripts
    {
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" />
        <link rel="stylesheet" href="~/css/jquery-ui.min.css"  />
        <style>
            span.field-validation-error {
                color: red;
            }
        </style>
        <script src="~/Scripts/jquery-3.1.1.min.js"></script>
        <script src="~/Scripts/jquery.validate.min.js"></script>
        <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
        <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
        <script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
    
    
        <script>
            $(document).ready(function () {
                var oTable = $('#OptMsgTypeTable').DataTable({
                    "ajax": {
                        "url": 'Settings/OptMsgType/GetMsgTypes',
                        "type": "get",
                        "datatype": "json"
                    },
                    "columns": [
                        { "data": "MsgTypeId", "autoWidth": true },
                        { "data": "MsgType", "autoWidth": true },
                        { "data": "MsgStatus", "autoWidth": true },
                        {
                            "data": "MsgTypeId", "width": "50px", "render": function (data) {
                                return '<a class="popup" href="/Settings/OptMsgType/Create/' + data + '">Edit</a>';
                            }
                        },
                        {
                            "data": "MsgTypeId", "width": "50px", "render": function (data) {
                                return '<a class="popup" href="/Settings/OptMsgType/Delete/' + data + '">Delete</a>';
                            }
                        }
                    ]
                })
                $('.tablecontainer').on('click', 'a.popup', function (e) {
                    e.preventDefault();
                    OpenPopup($(this).attr('href'));
                })
                function OpenPopup(pageUrl) {
                    var $pageContent = $('<div/>');
                    $pageContent.load(pageUrl, function () {
                        $('#popupForm', $pageContent).removeData('validator');
                        $('#popupForm', $pageContent).removeData('unobtrusiveValidation');
                        $.validator.unobtrusive.parse('form');
    
                    });
    
                    $dialog = $('<div class="popupWindow" style="overflow:auto"></div>')
                        .html($pageContent)
                        .dialog({
                            draggable: false,
                            autoOpen: false,
                            resizable: false,
                            model: true,
                            title: 'Popup Dialog',
                            height: 550,
                            width: 600,
                            close: function () {
                                $dialog.dialog('destroy').remove();
                            }
                        })
    
                    $('.popupWindow').on('submit', '#popupForm', function (e) {
                        var url = $('#popupForm')[0].action;
                        $.ajax({
                            type: "POST",
                            url: url,
                            data: $('#popupForm').serialize(),
                            success: function (data) {
                                if (data.status) {
                                    $dialog.dialog('close');
                                    oTable.ajax.reload();
                                }
                            }
                        })
    
                        e.preventDefault();
                    })
                    $dialog.dialog('open');
                }
            })
        </script>
    }
    

    Thursday, April 30, 2020 11:45 AM

All replies

  • User475983607 posted

    I googled it and it seems kind of server error. I would like to ask if I have way to debug the cause of error.

    Yes, debugging is the next step.  Your code is rather confusing as it returns JSON responses but you have not shared any AJAX or HttpClient making HTTP requests.  I assume the 500 error has something to do with code we cannot see. 

    Thursday, April 30, 2020 12:12 PM
  • User-1140746197 posted

    Sorry, just supplement this.

    Thank you.

    Thursday, April 30, 2020 12:30 PM
  • User753101303 posted

    Hi,

    See https://medium.stackify.com/iis-logs-error-logs-and-more-6-ways-to-find-failed-asp-net-requests-23b16e40db89 and point 3.

    It allows to know which exact error and then you can look at the relevant place from what could cause this error. When  reading it is not uncommon to see multiple possible errors without any way to know if or which one happens or that the problem is caused by some data you can't see just by reading your code.

    Thursday, April 30, 2020 2:31 PM
  • User1686398519 posted

    Hi,  thtang

    Because you have two get methods named "Create", although one has parameters and one doesn't, the "AmbiguousMatchException" error will appear. You can use "[HttpGet (" {id} ")]" before the Create method with parameters.

    More usage of " HttpGet " can refer to this link.

    https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1#attribute-routing-with-http-verb-attributes

    Here is the result.

    Best Regards,

    YihuiSun

    Monday, May 4, 2020 10:33 AM