C# Razor Page动态创建带属性的div
C# Razor Page Dynamically Create Divs with Attributes
我有以下 CSHTML 代码:
<div class="container-fluid projects padding-top-small">
<div class="row">
<div class="col-sm-6 col-md-3">
<div class="project-inner">
<a href="service1.html">
<img src="assets/img/portfolio/image1.jpg" alt="">
<div class="project-caption">
<div class="project-details">
<p><i class="fa fa-plus fa-lg"></i></p>
<h3>Service 1</h3>
<p><small>Short Description 1</small></p>
</div>
</div>
</a>
</div>
</div>
<div class="col-sm-6 col-md-3">
<div class="project-inner">
<a href="service2.html">
<img src="assets/img/portfolio/image2.jpg" alt="">
<div class="project-caption">
<div class="project-details">
<p><i class="fa fa-plus fa-lg"></i></p>
<h3>Service 2</h3>
<p><small>Short Description 2</small></p>
</div>
</div>
</a>
</div>
</div>
<div class="col-sm-6 col-md-3">
<div class="project-inner">
<a href="service1.html">
<img src="assets/img/portfolio/image3.jpg" alt="">
<div class="project-caption">
<div class="project-details">
<p><i class="fa fa-plus fa-lg"></i></p>
<h3>Service 3</h3>
<p><small>Short Description 3</small></p>
</div>
</div>
</a>
</div>
</div>
<div class="col-sm-6 col-md-3">
<div class="project-inner">
<a href="service1.html">
<img src="assets/img/portfolio/image4.jpg" alt="">
<div class="project-caption">
<div class="project-details">
<p><i class="fa fa-plus fa-lg"></i></p>
<h3>Service 4</h3>
<p><small>Short Description 4</small></p>
</div>
</div>
</a>
</div>
</div>
</div>
</div>
几乎所有内容都是硬编码的 HTML 个标签。
我希望能够动态呈现它们。
在我的控制器中,我正在检索由以下项目组成的服务对象列表:SERVICE_URL、SERVICE_IMAGE_URL、SERVICE_NAME 和 SERVICE_DESCRIPTION。这些列表存储在 ViewBag 中(不确定 viewbag 是否是最佳占位符)。
<div class="col-sm-6 col-md-3">
<div class="project-inner">
<a href="SERVICE_URL">
<img src="SERVICE_IMAGE_URL" alt="">
<div class="project-caption">
<div class="project-details">
<p><i class="fa fa-plus fa-lg"></i></p>
<h3>SERVICE_NAME</h3>
<p><small>SERVICE_DESCRIPTION</small></p>
</div>
</div>
</a>
</div>
</div>
我想要实现的是让每个 div 动态呈现,这样我就可以只显示那些可用的服务。
Controller.cs
public class myService
{
public string service_url { get; set; }
public string service_image_url { get; set; }
public string service_name { get; set; }
public string service_description { get; set; }
}
private void loadServices()
{
List<myService> myServices = new List<myService>();
for(int i=0; i<3; i++)
{
myService msvc = new myService();
msvc.service_url = "service_url" + i.ToString() + ".html";
msvc.service_image_url = "image_url" + i.ToString() + ".jpg";
msvc.service_name = "Service " + i.ToString();
msvc.service_description = "Service Description " + i.ToString();
myServices.Add(msvc);
}
ViewBag.DataServices = myServices;
}
public ActionResult Home()
{
loadServices();
return this.RedirectToAction("Index", "Controller");
}
改写问题:
在我的控制器上,我有一个服务列表并将它们存储到 ViewBag,我如何使用硬编码 HTML?
提供的属性在 Razor 页面上生成 DIV
我为此构建了一个工作原型,并将 运行 您完成每个部分的步骤。然后我建议你找一些关于 MVC Razor 编程的好基础书籍,并从头开始,因为有很多细节需要学习:
- 对任何 类 使用单独的文件并将模型放在模型文件夹中。
- 使用适当的 standards-based naming/proper-casing 属性(属性的前导大写 & 类,通常 "CamelCase")
例如Models\MyService.cs:
namespace WebApplication.Models
{
public class MyService
{
public string ServiceUrl { get; set; }
public string ServiceImageUrl { get; set; }
public string ServiceName { get; set; }
public string ServiceDescription { get; set; }
}
}
- 您的
Home
控制器需要 Services
操作,因此 URL 路由来自有意义的 /Home/Services
Controllers\HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication5.Models;
namespace WebApplication.Controllers
{
public class HomeController : Controller
{
private List<MyService> LoadServices()
{
List<MyService> myServices = new List<MyService>();
for (int i = 0; i < 3; i++)
{
MyService msvc = new MyService();
msvc.ServiceUrl = "service_url" + i.ToString() + ".html";
msvc.ServiceImageUrl = "image_url" + i.ToString() + ".jpg";
msvc.ServiceName = "Service " + i.ToString();
msvc.ServiceDescription = "Service Description " + i.ToString();
myServices.Add(msvc);
}
return myServices;
}
public ActionResult Services()
{
// return a view using the ViewModel provided by LoadServices()
return View(LoadServices());
}
}
}
- 在视图中,需要声明传递的视图模型类型
- 您需要遍历模型(这是一个可枚举的集合)
- 使用 Razor 语法注入模型元素的属性
Views\Home\Services.cshtml:
@model IEnumerable<WebApplication.Models.MyService>
<div class="container-fluid projects padding-top-small">
<div class="row">
@foreach (var service in Model)
{
<div class="col-sm-6 col-md-3">
<div class="project-inner">
<a href="@service.ServiceUrl">
<img src="@service.ServiceImageUrl" alt="">
<div class="project-caption">
<div class="project-details">
<p><i class="fa fa-plus fa-lg"></i></p>
<h3>@service.ServiceName</h3>
<p><small>@service.ServiceDescription</small></p>
</div>
</div>
</a>
</div>
</div>
}
</div>
</div>
所有这些工作的最终结果如下所示:
我有以下 CSHTML 代码:
<div class="container-fluid projects padding-top-small">
<div class="row">
<div class="col-sm-6 col-md-3">
<div class="project-inner">
<a href="service1.html">
<img src="assets/img/portfolio/image1.jpg" alt="">
<div class="project-caption">
<div class="project-details">
<p><i class="fa fa-plus fa-lg"></i></p>
<h3>Service 1</h3>
<p><small>Short Description 1</small></p>
</div>
</div>
</a>
</div>
</div>
<div class="col-sm-6 col-md-3">
<div class="project-inner">
<a href="service2.html">
<img src="assets/img/portfolio/image2.jpg" alt="">
<div class="project-caption">
<div class="project-details">
<p><i class="fa fa-plus fa-lg"></i></p>
<h3>Service 2</h3>
<p><small>Short Description 2</small></p>
</div>
</div>
</a>
</div>
</div>
<div class="col-sm-6 col-md-3">
<div class="project-inner">
<a href="service1.html">
<img src="assets/img/portfolio/image3.jpg" alt="">
<div class="project-caption">
<div class="project-details">
<p><i class="fa fa-plus fa-lg"></i></p>
<h3>Service 3</h3>
<p><small>Short Description 3</small></p>
</div>
</div>
</a>
</div>
</div>
<div class="col-sm-6 col-md-3">
<div class="project-inner">
<a href="service1.html">
<img src="assets/img/portfolio/image4.jpg" alt="">
<div class="project-caption">
<div class="project-details">
<p><i class="fa fa-plus fa-lg"></i></p>
<h3>Service 4</h3>
<p><small>Short Description 4</small></p>
</div>
</div>
</a>
</div>
</div>
</div>
</div>
几乎所有内容都是硬编码的 HTML 个标签。
我希望能够动态呈现它们。
在我的控制器中,我正在检索由以下项目组成的服务对象列表:SERVICE_URL、SERVICE_IMAGE_URL、SERVICE_NAME 和 SERVICE_DESCRIPTION。这些列表存储在 ViewBag 中(不确定 viewbag 是否是最佳占位符)。
<div class="col-sm-6 col-md-3">
<div class="project-inner">
<a href="SERVICE_URL">
<img src="SERVICE_IMAGE_URL" alt="">
<div class="project-caption">
<div class="project-details">
<p><i class="fa fa-plus fa-lg"></i></p>
<h3>SERVICE_NAME</h3>
<p><small>SERVICE_DESCRIPTION</small></p>
</div>
</div>
</a>
</div>
</div>
我想要实现的是让每个 div 动态呈现,这样我就可以只显示那些可用的服务。
Controller.cs
public class myService
{
public string service_url { get; set; }
public string service_image_url { get; set; }
public string service_name { get; set; }
public string service_description { get; set; }
}
private void loadServices()
{
List<myService> myServices = new List<myService>();
for(int i=0; i<3; i++)
{
myService msvc = new myService();
msvc.service_url = "service_url" + i.ToString() + ".html";
msvc.service_image_url = "image_url" + i.ToString() + ".jpg";
msvc.service_name = "Service " + i.ToString();
msvc.service_description = "Service Description " + i.ToString();
myServices.Add(msvc);
}
ViewBag.DataServices = myServices;
}
public ActionResult Home()
{
loadServices();
return this.RedirectToAction("Index", "Controller");
}
改写问题: 在我的控制器上,我有一个服务列表并将它们存储到 ViewBag,我如何使用硬编码 HTML?
提供的属性在 Razor 页面上生成 DIV我为此构建了一个工作原型,并将 运行 您完成每个部分的步骤。然后我建议你找一些关于 MVC Razor 编程的好基础书籍,并从头开始,因为有很多细节需要学习:
- 对任何 类 使用单独的文件并将模型放在模型文件夹中。
- 使用适当的 standards-based naming/proper-casing 属性(属性的前导大写 & 类,通常 "CamelCase")
例如Models\MyService.cs:
namespace WebApplication.Models
{
public class MyService
{
public string ServiceUrl { get; set; }
public string ServiceImageUrl { get; set; }
public string ServiceName { get; set; }
public string ServiceDescription { get; set; }
}
}
- 您的
Home
控制器需要Services
操作,因此 URL 路由来自有意义的/Home/Services
Controllers\HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication5.Models;
namespace WebApplication.Controllers
{
public class HomeController : Controller
{
private List<MyService> LoadServices()
{
List<MyService> myServices = new List<MyService>();
for (int i = 0; i < 3; i++)
{
MyService msvc = new MyService();
msvc.ServiceUrl = "service_url" + i.ToString() + ".html";
msvc.ServiceImageUrl = "image_url" + i.ToString() + ".jpg";
msvc.ServiceName = "Service " + i.ToString();
msvc.ServiceDescription = "Service Description " + i.ToString();
myServices.Add(msvc);
}
return myServices;
}
public ActionResult Services()
{
// return a view using the ViewModel provided by LoadServices()
return View(LoadServices());
}
}
}
- 在视图中,需要声明传递的视图模型类型
- 您需要遍历模型(这是一个可枚举的集合)
- 使用 Razor 语法注入模型元素的属性
Views\Home\Services.cshtml:
@model IEnumerable<WebApplication.Models.MyService>
<div class="container-fluid projects padding-top-small">
<div class="row">
@foreach (var service in Model)
{
<div class="col-sm-6 col-md-3">
<div class="project-inner">
<a href="@service.ServiceUrl">
<img src="@service.ServiceImageUrl" alt="">
<div class="project-caption">
<div class="project-details">
<p><i class="fa fa-plus fa-lg"></i></p>
<h3>@service.ServiceName</h3>
<p><small>@service.ServiceDescription</small></p>
</div>
</div>
</a>
</div>
</div>
}
</div>
</div>
所有这些工作的最终结果如下所示: