gpt4 book ai didi

c# - 如何在 Asp.net MVC 5 中填充没有 Entity Framework 的模型

转载 作者:太空狗 更新时间:2023-10-29 22:22:16 25 4
gpt4 key购买 nike

注意:- 对entity framework一无所知,直接开始学习asp.net MVC 5的开发者会出现这个问题。一旦你走上了学习的道路一个新事物很难把握好几个新概念。这个问题将帮助新开发人员利用他们现有的 Ado.net 知识立即开始使用 MVC。

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

    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>

最佳答案

你快到了。你的 Controller 看起来像这样:

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; }
}
}

你的 View 看起来像这样:

@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>

希望这对您有所帮助。

关于c# - 如何在 Asp.net MVC 5 中填充没有 Entity Framework 的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29576138/

25 4 0
文章推荐: c# - 如何处理时间耦合?
文章推荐: python - 狮身人面像文档 : how to reference a Python property?
文章推荐: c# - 在 View 模型中使用 Caliburn 的 conductor 时窗口标题被覆盖