繁体   English   中英

如何在Asp.net MVC 5中填充没有实体框架的模型

[英]How to populate a model without entity framework in Asp.net MVC 5

注意: - 这个问题将出现在开发人员的脑海中,他们对实体框架一无所知,并且直接开始学习asp.net MVC 5.一旦你开始学习新东西,就很难掌握几个新概念。 这个问题将帮助新开发人员使用他们现有的Ado.net知识立即开始使用MVC。

我一直在尝试使用现有数据库的asp.net MVC 5。 我不打算使用Entity框架来填充模型。 如何在不使用任何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>

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

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>

希望这可以帮助。

你可以在一个动作中做任何你想做的事。 在合理范围内,它只是一种方法,就像任何其他方法一样。 如果您使用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);
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM