这里我面临的问题是,我在数据搜索框中插入文本后,我正在做服务器端dataTable过滤器内容单引号,例如 - (一个或多个实体的验证失败。请参阅'EntityValidationErrors'属性更多详细信息。)如果我在搜索文本框中提供此文本并且我尝试从服务器端进行过滤,那么我就会收到错误。
在下面我提到我的动作方法代码
string searchValue = Request["search[value]"];
searchValue = searchValue.Trim();
v = db.Database.SqlQuery<ListViewModel>("select EL.Id,USR.FullName as UsrName,case when EL.E_Message is null or EL.E_Message = '' then '--Message Not Found--' else EL.E_Message end as E_Message,case when EL.InnerException is null or EL.InnerException = '' then '--Not Found--' else EL.InnerException end as InnerException,EL.DateTimeErrorLogged, case when C.name is null then '--No Company Found--' else C.name end as company from s_ErrorLogs EL inner join AspNetUsers USR on EL.UserID = USR.Id Left join company C on EL.Company_Id = C.id where USR.FullName LIKE '%'" + searchValue + "'%' or E_Message LIKE '%'" + searchValue + "'%' or EL.InnerException LIKE '%'" + searchValue + "'%' or EL.DateTimeErrorLogged LIKE '%'" + searchValue + "'%' or C.name LIKE '%'" + searchValue + "'%' order by EL.Id");
答案 0 :(得分:0)
尝试替换包含&#39;的字符串(1个单引号)&#39;&#39; (2个单引号)......
string searchValue = Request["search[value]"];
searchValue = searchValue.Trim();
v = db.Database.SqlQuery<ListViewModel>("select EL.Id,USR.FullName as UsrName,case when EL.E_Message is null or EL.E_Message = '' then '--Message Not Found--' else EL.E_Message end as E_Message,case when EL.InnerException is null or EL.InnerException = '' then '--Not Found--' else EL.InnerException end as InnerException,EL.DateTimeErrorLogged, case when C.name is null then '--No Company Found--' else C.name end as company from s_ErrorLogs EL inner join AspNetUsers USR on EL.UserID = USR.Id Left join company C on EL.Company_Id = C.id where USR.FullName LIKE '%'" + searchValue.Replace(''','''') + "'%' or E_Message LIKE '%'" + searchValue.Replace(''','''') + "'%' or EL.InnerException LIKE '%'" + searchValue.Replace(''','''') + "'%' or EL.DateTimeErrorLogged LIKE '%'" + searchValue.Replace(''','''') + "'%' or C.name LIKE '%'" + searchValue.Replace(''','''') + "'%' order by EL.Id");
答案 1 :(得分:0)
我修改了你的代码,现在它的工作感谢,例如我为其他人展示-----其中USR.FullName LIKE'%“+ searchValue.Replace(”'“,”''“)+”%'或E_Message LIKE'%“+ searchValue.Replace(”'“,”''“)+”%'