【问题标题】:How to search with parameter in MVC5 without ef如何在没有 ef 的情况下在 MVC5 中使用参数进行搜索
【发布时间】:2016-09-19 11:53:06
【问题描述】:

如果有人可以帮助我了解如何在不使用实体框架的情况下在 MVC 5 中创建搜索页面,我会很高兴。如果用户输入产品名称并发布,结果将显示在GridViewInputBox/Label 中(单行)。我在后端使用 C# 和 SQLServer。我只是 MVC 和 C# 的初学者。

我创建了一个如下图的控制器;

{
  connect pcon = new connect();
  using (SqlConnection conn = new SqlConnection(pcon.CS))
  {
    try
    {
      //do something
      pcon.cmd.CommandText = "SELECT * FROM tbproduct WHERE itemName=@name)";
      pcon.cmd.Parameters.Add(new SqlParameter("@name", System.Data.SqlDbType.NVarChar, 50));
      pcon.cmd.Parameters["@name"].Value = p.itemName;
      pcon.cmd.Connection = conn;
      conn.Open();
      datagridview = pcon.cmd.ExecuteReader();
      TempData["Success"] = "Record Added Sucessfully.";
      return RedirectToAction("Index", "Product");
    }
    catch (Exception ex)
    {
      // throw ex;
      // return RedirectToAction("Index", "Product");
      return View("Error", new HandleErrorInfo(ex, "Product", "AddProduct"));
    }    // end of try-catch
  }      // end of using
}        // end of spurious open bracket

有了这个,我不知道如何将它传递给视图以获得有意义的结果。 谢谢

【问题讨论】:

  • 在mvc中没有gridview。
  • // 虚假开括号结束 - 大声笑

标签: c# .net sql-server asp.net-mvc model-view-controller


【解决方案1】:

实际上你的控制器方法需要带一些搜索参数来搜索并返回结果。

您还可以根据要在视图上显示的属性/字段创建视图模型,然后将结果映射到视图模型并返回。

public ActionResult DoSearch(string searchString) 
{           
    var movies = <get the movie list>; 

    if (!String.IsNullOrEmpty(searchString)) 
    { 
        movies = movies.Where(s => s.Title.Contains(searchString)); 
    }

    return View(movies); 
}

在视图中,您可以通过循环模型来创建表格以显示结果。

@model IEnumerable<MvcMovie.Models.Movie> 
@  
if (Model.Count() == 0)
{
  <tr>
  <td colspan = "3" >Records not found</td>  
  </tr >  
}
else 
{      
    foreach(var item in Model) 
    { 
    <tr>
    <td> @Html.DisplayFor(modelItem = > item.MovieName)</td>            
    <td> @Html.DisplayFor(modelItem => item.ReleaseDate)</td>          
    </tr >  
    }  
}

为了显示更有吸引力和用户友好的表格,您也可以使用IPagedList

看看http://www.asp.net/mvc/overview/getting-started/introduction/adding-search 教程。

【讨论】:

    【解决方案2】:

    您可以从ExecuteReader 对象中获取结果并将其传递给您的视图。我还建议对应用程序的架构进行一些更改。

    首先,我将有一个 ViewModel 类,它捕获您希望传递给视图的实体。比如:

    public class ProductViewModel
    {
        private ProductService _productService = new ProductService();
    
        public IEnumerable<Product> GetProducts(string name)
        {
           return _productService.GetProducts(name);
        }
    }
    

    我建议将 ADO.NET 代码(SqlConnection、SqlCommand)放入不同的层,即数据访问层或 ProductService。

    public class ProductService
    {
        public IEnumerable<Products> GetProducts(string name) 
        {
           List<Products> products = new List<Products>();
           using (SqlConnection conn = new SqlConnection(pcon.CS))
           {
              // Your code here
              // Build up the products list from the SQL data reader
           }
           return products;
        }
    }
    

    这将使您的控制器保持轻便且不受业务逻辑的影响。

    在您的控制器中,您将拥有:

    public ActionResult GetProduct(string product)
    {
        ProductViewModel model = new ProductViewModel();
        var products = model.GetProducts(product);
    
        return View(products);
    }
    

    然后在您的视图 (.cshtml) 文件中,您将遍历 IEnumerable 集合(您没有像网格视图这样的服务器端控件),因此您构建自己的。

    @model IEnumerable<Product>
    @foreach(var product in Model)
    {
       @product.name
    }
    

    作为附加说明,您可能需要考虑使用 ORM,例如 Entity Framework 或 nHibernate,它们可以为您完成所有令人振奋的工作。

    【讨论】:

    • 非常感谢达伦。我遵循了你的程序,但我有点困惑。这是我的产品视图模型:命名空间 POS.Models { public class ProductViewModel { private ProductService _productService = new ProductService();公共 IEnumerable GetProducts(string name) { return _productService.GetProducts(name);这是我的产品服务: public IEnumerable GetProducts(string name) { List products = new List();使用 (SqlConnection conn = new SqlConnection(pcon.CS))
    • 产品服务模型续..{ pcon.cmd.CommandText = "SELECT * FROM tbproduct WHERE itemName=@name)"; pcon.cmd.Parameters.Add(new SqlParameter("@name", System.Data.SqlDbType.NVarChar, 50)); pcon.cmd.Parameters["@name"].Value =name; pcon.cmd.Connection = conn;使用 (SqlDataReader reader = pcon.cmd.ExecuteReader()) { while (reader.Read()) { products.Add(new productModel() { Description = reader.GetString(reader.GetOrdinal("itemDesc")), quantity = reader.GetInt32(reader.GetOrdinal("quantity")), } reader.Close(); return products;
    • 这是我的控制器: public ActionResult GetProduct(string product) { ProductViewModel model = new ProductViewModel(); var products = model.GetProducts(product);返回视图(产品); }
    • 这是我的观点:(at)model IEnumerable (at){ ViewBag.Title = "Index"; }

      索引

      (at)foreach (var product in Model) { (at)product.itemName }
    • 如果阅读困难,请指导我如何使用我的代码发送评论。我使用 (at) 来表示视图中的 at 符号。我的问题是;我看不到输入参数 (productName) 如何传递给控制器​​以获取要显示在视图上的搜索结果。我在想 http_post 将用于将搜索键发送到控制器,控制器会将搜索结果从模型中提取回视图以显示给用户。但我无法想象这一点。请原谅我,我相信是因为是新用户造成的。非常感谢
    猜你喜欢
    • 2023-04-09
    • 2020-01-08
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    相关资源
    最近更新 更多