我是ASP.Net MVC的新手。我正在尝试从查询字符串的ID中获取硬编码数据。
我正在开发一个简单的ASP.Net Web项目。有些客户数据是经过硬编码的。通过单击超链接进行重定向时,我需要在另一个页面中检索和显示数据。
// CustomerController
public ActionResult Details(int id) {
var viewModel = new RandomMovieViewModel {
CustomerID = id,
CustomerName = "Test"
};
return View(viewModel);
}
// RandomViewModel类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Vidly.Models;
namespace Vidly.ViewModels
{
public class RandomMovieViewModel
{
public Movie Movie { get; set; }
public List<Customer> Customers { get; set; }
public List<Movie> Movies { get; set; }
public int CustomerID { get; set; }
public string CustomerName { get; set; }
}
}
// Index.cshtml
@model Vidly.ViewModels.RandomMovieViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Customers</h2>
@if (Model.Customers.Count == 0)
{
<text>We don't have any customers yet.</text>
}
else
{
<ul class="list-group">
<li class="list-group-item d-flex justify-content-between align-items-center">
<b>Customer</b>
</li>
@foreach (var customer in Model.Customers)
{
<li class="list-group-item d-flex justify-content-between align-items-center">
<a href="../Customers/Details/@customer.Id">
@customer.Name
</a>
</li>
}
</ul>
}
Details.cshtml
@model Vidly.ViewModels.RandomMovieViewModel
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@Model.CustomerName</h2>
单击超链接时,应显示选定的客户名称。
答案 0 :(得分:0)
@foreach (var customer in Model.Customers)
{
<li class="list-group-item d-flex justify-content-between align-items-center">
<a href="../Customers/Details/@customer.Id">
@customer.Name
</a>
</li>
}
要从查询字符串的视图中获取ID值,您需要使用
来增强代码 <a href="../Customers/Details?id=@customer.Id">