【问题标题】:How to populate a model without entity framework in Asp.net MVC 5如何在 Asp.net MVC 5 中填充没有实体框架的模型
【发布时间】:2015-04-11 09:15:24
【问题描述】:

注意:- 这个问题会出现在对实体框架没有概念,直接开始学习 asp.net MVC 5 的开发者的脑海中。一旦你在学习的路上一个新事物很难把握几个新概念。这个问题将帮助新开发人员利用他们现有的 Ado.net 知识立即开始使用 MVC。

我一直在尝试将 asp.net MVC 5 与我现有的数据库一起使用。我不会使用实体框架来填充模型。如何在不使用任何 dbcontext 的情况下填充我的模型。如何从数据库中获取数据并放入模型中? 这是我的控制器操作:-

    public ActionResult Index()
            {
                /* WHAT TO DO HERE TO LOAD THE TRANSACTIONS? This part is resolved in following lines*/
/* I have used following code to get it using the direct db connection */
var transactions = new List<Portal.Models.TransactionModels>();
            var connectionString = "server=yourserver;database=yourdatabase;uid=youruid;pwd=yourpwd";
            MySqlConnection oMySqlConnection = new MySqlConnection(connectionString);
            oMySqlConnection.Open();
            MySqlCommand oMySqlCommand = new MySqlCommand("Select * from yourtable;", oMySqlConnection);
            MySqlDataReader oMySqlDataReader = oMySqlCommand.ExecuteReader();
            while (oMySqlDataReader.Read())
            {
                transactions.Add(new Portal.Models.TransactionModels
                {
                    transaction_id = oMySqlDataReader["transaction_id"].ToString()
                });
            }
            oMySqlConnection.Close();
            return View(transactions);

                return View();
            }

这是我的模型:-

  public class TransactionModels
    {
        public virtual string id{ get; set;}
        public virtual int statusid { get; set; }
    }

这是我的观点:-

@model IEnumerable<Portal.Models.TransactionModels>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.statusid)
        </th>       
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.id)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.statusid)
        </td>

        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>

【问题讨论】:

  • 还有其他地方会出现问题吗?只是想知道
  • 我不太确定我知道你在问什么。看起来你最初是在寻找如何编写一个动作,但现在你似乎或多或少已经完成了一个动作。最新编辑后您有什么问题?
  • 你是对的,马修,实际上问题被搁置了,所以我为什么还要在问题中添加我的答案

标签: c# asp.net asp.net-mvc


【解决方案1】:

你快到了。你的控制器看起来像这样:

public ActionResult Index()
{
  List<BillingValidation> list = new List<BillingValidation>();
  try
  {
    // runs stored procedure and returns data to main page
    using (SqlConnection con = new SqlConnection())
    {
      String sql = @"yourquery";
      con.ConnectionString = @"yourconnection"

      DataTable dt = new DataTable();
      SqlDataAdapter da = new SqlDataAdapter();
      da.SelectCommand = new SqlCommand(sql, con);

      da.Fill(dt);

      foreach (DataRow row in dt.Rows)
      {
        var bilVal = new BillingValidation();
        bilVal.Total = row["Total"].ToString();
        bilVal.Section = row["Section"].ToString();
        bilVal.Details = row["Details"].ToString();
        list.Add(bilVal);
      }
    }
    return View(list);
  }
}

对于看起来像这样的模型:

using System.Data.Entity;
using System.Security.Claims;
using System.ComponentModel.DataAnnotations;

namespace SO_GUI.Models
{    
    public class BillingValidation
    {

        [Key]
        public string Section { get; set; }

        public string Details { get; set; }

        public string Total { get; set; }
    }    
}

您的视图将如下所示:

@model IEnumerable<SO_GUI.Models.BillingValidation>

@{
    ViewBag.Title = "Index";
}

<h2>Billing Validation</h2>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Section)
        </th> 
        <th>
            @Html.DisplayNameFor(model => model.Details)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Total)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Section)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Details)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Total)
        </td>
    </tr>
}
</table>

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    你可以在一个动作中做任何你想做的事情。在合理的范围内,它只是一种方法,就像任何其他方法一样。如果您使用 MVC,您将如何获取数据?

    听起来您正在寻找使用 ADO.net 的东西,类如 SqlConnectionSqlCommand

    使用 ADO.net 非常简单,虽然有时有点混乱,所以我很想将它拉到自己的层中。但无论如何,编写该代码超出了这个问题的范围,鉴于您提供给我们的信息,询问如何编写代码过于宽泛。

    不过,简而言之,您可以在该方法中做任何您想做的事情。忽略数据访问部分,您可以从这里构建。

    public ActionResult Index()
    {
        var transactions = new List<Portal.Models.TransactionModels>();
    
        transactions.Add(new Portal.Models.TransactionModels{
            id = "1",
            statusId = 4
        })
        transactions.Add(new Portal.Models.TransactionModels{
            id = "2",
            statusId = 3
        })
    
        return View(transactions);
    }
    

    如果你把它放进去,你的视图应该被这两条假线填充。

    【讨论】:

    • 谢谢马修,让我试一试。我将使用简单的 Ado.net 获取具有值的数据表,并尝试使用该数据表构建交易列表
    猜你喜欢
    • 2014-01-18
    • 2011-07-22
    • 2011-12-06
    • 2019-02-17
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多