Asp.net mvc 如何使用ASP.NET Core 3.0将数据从控制器发送到cshtml
我正在使用一个自己的数据库,我希望从数据库中获取描述,并将数据放入html表格中。 但是我找不到任何有用的东西,也不知道如何将数据从控制器传输到视图,或者如何将数据从视图请求到控制器 这就是我的控制器的外观:Asp.net mvc 如何使用ASP.NET Core 3.0将数据从控制器发送到cshtml,asp.net-mvc,asp.net-core,model-view-controller,razor,controller,Asp.net Mvc,Asp.net Core,Model View Controller,Razor,Controller,我正在使用一个自己的数据库,我希望从数据库中获取描述,并将数据放入html表格中。 但是我找不到任何有用的东西,也不知道如何将数据从控制器传输到视图,或者如何将数据从视图请求到控制器 这就是我的控制器的外观: using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Test.Models; namespace
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Test.Models;
namespace Test.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult getTableNames()
{
DatabaseItemDescription databaseItemDescription = new DatabaseItemDescription();
databaseItemDescription.Name = "abc";
databaseItemDescription.Description = "def";
databaseItemDescription.Content = "ghi";
return View(databaseItemDescription); //Does this work?
}
}
}
@page
@model DatabaseItemDescription
<h1>DatabaseEditTest</h1>
<table id="buttonTable">
<thead>
</thead>
</table>
<table id="descriptionTable"
class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>Action</th>
<th>Name</th>
<th>Description</th>
<th>Content</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
function getFirstTableDesc() {
$("#firstTable thead").remove();
$("#buttonTable thead").append("<tr>" + "<th><button onclick='updateTableDesc()'>Update</button></td>" + "<th><button onclick='refreshTableDesc()'>Refresh</button></td>" + "<tr>");
}
function updateTableDesc() {
$("#descriptionTable tbody").empty();
//here i want to read the Data from the Controller and insert it into the table like:
//Fetch Data from Controller
//$("#descriptionTable tbody").append("<tr>" + "<td><button onclick='Edit()'>Edit</button></td>" + "<td>${Name}</td>" + "<td>${Description}</td>" + "<td>${Content}</td>" + "</tr>");
}
}
function refreshTableDesc() {
}
</script>
我的Index.cshtml如下所示:
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Test.Models;
namespace Test.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult getTableNames()
{
DatabaseItemDescription databaseItemDescription = new DatabaseItemDescription();
databaseItemDescription.Name = "abc";
databaseItemDescription.Description = "def";
databaseItemDescription.Content = "ghi";
return View(databaseItemDescription); //Does this work?
}
}
}
@page
@model DatabaseItemDescription
<h1>DatabaseEditTest</h1>
<table id="buttonTable">
<thead>
</thead>
</table>
<table id="descriptionTable"
class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>Action</th>
<th>Name</th>
<th>Description</th>
<th>Content</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
function getFirstTableDesc() {
$("#firstTable thead").remove();
$("#buttonTable thead").append("<tr>" + "<th><button onclick='updateTableDesc()'>Update</button></td>" + "<th><button onclick='refreshTableDesc()'>Refresh</button></td>" + "<tr>");
}
function updateTableDesc() {
$("#descriptionTable tbody").empty();
//here i want to read the Data from the Controller and insert it into the table like:
//Fetch Data from Controller
//$("#descriptionTable tbody").append("<tr>" + "<td><button onclick='Edit()'>Edit</button></td>" + "<td>${Name}</td>" + "<td>${Description}</td>" + "<td>${Content}</td>" + "</tr>");
}
}
function refreshTableDesc() {
}
</script>
@page
@模型数据库项描述
数据库编辑测试
行动
名称
描述
内容
函数getFirstTableDesc(){
$(“#firstTable thead”).remove();
$(“#按钮表thead”).append(“+”更新“+”刷新“+”);
}
函数updateTableDesc(){
$(“#descriptionTable tbody”).empty();
//在这里,我想从控制器读取数据并将其插入表中,如下所示:
//从控制器获取数据
//$(“#descriptionTable tbody”)。追加(“+”编辑“+”${Name}”+“${Description}”+“${Content}”+”);
}
}
函数refreshttabledesc(){
}
如果有人能帮助我,我会非常感激的!
谢谢试试这个:
<tr>
<th>@Model.Action</th>
<th>@Model.Name</th>
<th>@Model.Description</th>
<th>@Model.Content</th>
</tr>
@模型.行动
@型号.名称
@型号.说明
@模型.内容
如果代码中有强类型视图(使用小写形式的@model
wothm
)
您可以在视图中使用模型
属性来读取来自控制器
的信息
返回视图(databaseItemDescription)//这行吗
您正在将此对象作为模型
传递给视图
。您可以访问此信息myModel
,因此,您可以尝试以下操作:
<tr>
<th> Maybe you add some links here to perform actions like edit/delete </th>
<th>@Model.Name</th>
<th>@Model.Description</th>
<th>@Model.Content</th>
</tr>
也许你可以在这里添加一些链接来执行编辑/删除之类的操作
@型号.名称
@型号.说明
@模型.内容
它只是从控制器中读取信息,并将其显示在视图中。如果需要,您可以添加更多属性。您已经混合了这些内容,有两种方法可以更改并使其生效,如果您希望视图按照@Felipe Oriani的建议进行更改,您还需要更改索引方法并将数据作为模型返回
public IActionResult Index()
{
DatabaseItemDescription databaseItemDescription = new DatabaseItemDescription();
//prepare model
return View(databaseItemDescription);
}
然后可以使用@Modal.Name、@Model.Description等进行绑定
另一种方法是使用Ajax调用和绑定响应从JS获取数据,如下所示
function updateTableDesc() {
//here i want to read the Data from the Controller and insert it into the table like:
//Get data using Ajax call to "Home/getTableNames" and bind using JS
}
并将getTableNames
方法更改为返回Json
public IActionResult getTableNames()
{
DatabaseItemDescription databaseItemDescription = new DatabaseItemDescription();
//prepare model
return Json(databaseItemDescription); //this will work
}
好的,关于您的案例,有一个非常好的插件,名为,您可以使用它从
控制器
在视图
上显示数据。您可以参考以下代码片段了解您的需求:
您的型号
:
namespace Test.Models
{
public class DatabaseItemDescription
{
public string Name { get; set; }
public string Description { get; set; }
public string Content { get; set; }
}
}
您的控制器
:
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Test.Models;
namespace Test.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public JsonResult getTableNames()
{
List<DatabaseItemDescription> databaseItemDescription = new List<DatabaseItemDescription>();
//Example of how you could initialize your list
databaseItemDescription.Add(new DatabaseItemDescription
{
Name = "abc",
Description = "def",
Content = "ghi"
});
databaseItemDescription.Add(new DatabaseItemDescription
{
Name = "efg",
Description = "hij",
Content = "klm"
});
return Json(databaseItemDescription, JsonRequestBehavior.AllowGet);
}
}
}
对于刷新和更新,您可以参考插件的官方文档,也可以创建自定义按钮,并使用
AJAX
将它们绑定到Controller
方法。我希望这对您有所帮助。这也适用于列表吗?因为我将有一个大约15个条目的列表,这就是为什么我对如何在表中插入数据感到困惑的原因,我已经更新了我的答案。这里我们讨论如何从控制器发送数据以查看和显示它。要坚持下去,我想最好还有一个问题。