简体   繁体   中英

Code throws System.InvalidOperationException

I am trying to submit a form and i run into a 500 internal server error. When i debug the piece of code that is causing this throws an System.InvalidOperationException:

var description = _context.Malfunction.Single(d => d.Description == newReport.ReportDescsription).ToString();

this is my jquery for the description var:

 $(document).ready(function () {
        vm = {
            movieIds: [],
            ReportDescription: null
        };
function applyChanges() {
        vm.ReportDescription = document.getElementById("descriptionReport").value;
        if (vm.ReportDescription == "") {
            vm.ReportDescription = null;
        }
    }

ajax post:

 $.ajax({
 url: "/api/Malfunctions",
 method: "post",
 data: vm
 })
var description = _context.Malfunction.Select(des => desc.Description).FirstOrDefault(d => d.Description == newReport.ReportDescsription);
if(description != null)
{
   var str = description.toString();
}

Here you will first verify if the data is returned or is it null, if not null then you can continue with your code.

Edit: Added Select to only return description, assuming Malfunction has more columns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM