如何在asp.net MVC 3上将传递字符串paramatre转换为int



我是一名Asp.net MVC开发人员,我在将字符串转换为int时遇到了问题,因为我在搜索时发现了它。当我尝试在Contoller上编写此代码时:

public ViewResult Search(string textboxmvc)
{
    var student = from i in db.StudentSet  select i;
    if (!String.IsNullOrEmpty(textboxmvc))
    {
        student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                                   || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==int.Parse(textboxmvc));
    }
     return View(student);
}

它显示了这个问题:

Server Error in '/' Application.
LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedException: LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression.
Source Error:

Line 42:                     </tr>
Line 43: 
Line 44:                 <% foreach (var item in Model) { %>
Line 45:                     <tr>
Line 46:                    

Source File: d:aimenProjectsSchoo_ProjectSchoo_ProjectViewsStudentsSearch.aspx    Line: 44 

您必须事先解析它。

public ViewResult Search(string textboxmvc)
{
    int parsedId;
    int.TryParse(textboxmvc, out parsedId);
    var student = from i in db.StudentSet  select i;
    if (!String.IsNullOrEmpty(textboxmvc))
    {
        student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                                   || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==parsedId);
    }
     return View(student);
}

首先将其分配给另一个变量。

if (!String.IsNullOrEmpty(textboxmvc))
{
    var textboxmvcAsInt = int.Parse(textboxmvc);
    student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                               || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==textboxmvcAsInt);
}

试试这个:

public ViewResult Search(string textboxmvc)
{
    var student = from i in db.StudentSet  select i;
    if (!String.IsNullOrEmpty(textboxmvc))
    {
        int val = int.Parse(textboxmvc);
        student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                                   || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==val);
    }
     return View(student);
}

将转换置于LINQ表达式之外:

var numericValue = int.Parse(textboxmvc);
student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                           || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==numericValue);
    }

将文本框内容分配到LINQ to Entities查询之外的int

if (!String.IsNullOrEmpty(textboxmvc))
{
    var id = int.Parse(textboxmvc)
    student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                               || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())
                               || s.Id == id);
}
LINQ不知道如何将Int.Parse转换为SQL代码。执行强制转换并在查询之前将其分配给一个变量。
 if (!String.IsNullOrEmpty(textboxmvc))
 {
    int i = int.Parse(textboxmvc);
    student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())     
                 ||    s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==i);
  }

我认为你必须在Where之前进行转换,如下所示:

if (!String.IsNullOrEmpty(textboxmvc))
    {
        var number = int.Parse(textboxmvc);
        student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                                   || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==number);
    }

相关内容

  • 没有找到相关文章

最新更新