【问题标题】:ASP.NET MVC 4 searching from existing databaseASP.NET MVC 4 从现有数据库中搜索
【发布时间】:2013-09-24 09:51:59
【问题描述】:

我想知道是否有人可以帮助我在现有数据库上创建数据上下文并从中进行搜索。

到目前为止我做了什么:

  1. 为 web.config 上的现有数据库创建连接字符串(与我新创建的 DataContext 类同名)
  2. 为它制作了 DataContext 类和模型类,我想要获取的字段在哪里。
  3. 为它制作了需要搜索的控制器
  4. 为控制器制作视图

这是我使用的代码。

数据上下文类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace KendoUIMvcCim.Models
{
    public class CustDataContext : DbContext
    {
        public DbSet<Contacts> CLIENT { get; set; }

    }
}

我要搜索的信息的模型类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace KendoUIMvcCim.Models
{
    public class Contacts
    {
        [Key]
        public int CLIENT_ID { get; set; }
        public string FIRSTNAME { get; set; }
        public string LASTNAME { get; set; }
        public string TEL_TEL1 { get; set; }
        public string TEL_TEL2 { get; set; }
        public string TEL_GSM { get; set; }
    }
}

控制器

public ActionResult Testi(int? clientid)
        {
            using (var db = new CustDataContext()) 
         {
           var contact = db.CLIENT.Find(clientid);

             if (contact != null)
             {
                 return View(contact);
             }
             else
             {
                 return RedirectToAction("Index", "Customer");
             }

         }

任何帮助将不胜感激!

最好的问候, 埃罗

【问题讨论】:

  • 这帮助了我正确的方式,无法让它与手动制作 DBContexts 一起工作。但是在使用 ADO.NET 实体数据模型之后,它就可以工作了。我一开始不想这样做,因为我只想要一张大表中的几列,但我猜现在必须这样做。谢谢!

标签: c# asp.net asp.net-mvc-4 entity-framework-4 ef-code-first


【解决方案1】:

像这样使用 Linq:

    public ActionResult Index(string searchTerm = null)
    {

        var model =
            _db.Clients
            .Where(r => searchTerm == null || r.FirstName.StartsWith(searchTerm) || r.LastName.StartsWith(searchTerm))
                .Take(10)
                .Select r;

        return View(model);
    }

索引视图可能是这样的:

@model IEnumerable<AppName.Models.ModelName>

@{
   ViewBag.Title = "Home Page";
}

<form method="GET">
    <input type="search" name="searchTerm" />
    <input type="submit" value="Search for a name"/>
</form>
@try
{
    foreach (var item in Model)
    {
        <h3>@Html.DisplayFor(modelItem => item.FirstName)</h3>
        <p>@Html.DisplayFor(modelItem => item.LastName)</p>

    }
}
catch (NullReferenceException nullex)
{
    <p>@nullex</p>
}

【讨论】:

  • 感谢您的回复,抱歉回复晚了!
  • 不客气。 :)
【解决方案2】:

在类型列表中搜索任何属性的通用方法

public static IEnumerable<T> SearchColumns<T>(this IEnumerable<T> obj, string searchkey)
        {
            var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance);
            if (properties == null)
                throw new ArgumentException("{typeof(T).Name}' does not implement a public get property named '{key}.");
            var filteredObj = obj.Where(d => properties.Any(p => p.GetValue(d).ToString().Contains(searchkey))).ToList();
            return filteredObj;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-27
    • 2015-03-07
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多