MVC - 如何显示列表?
MVC - How to display a list?
我是 MVC 的新手,我正在尝试在 MVC 视图中填充项目列表 class,但是在启动期间模型对象在 .cshtml 文件中为 null。
@foreach (var element in Model)
感谢您的帮助。
我的代码:
public class HomeController : Controller
{
List<ModelMath> mathList = new List<ModelMath>();
[HttpPost]
public ActionResult Submit(FormCollection fc)
{
mathList = new List<ModelMath>();
int num = Convert.ToInt32(fc["Num"]);
while(num > 1)
{
ModelMath modelMath = new ModelMath();
modelMath.Number = num;
mathList.Add(modelMath);
num--;
}
return View(mathList);
}
}
型号class:
public class ModelMath
{
public int Number { get; set; }
}
Index.cshtml
@{
ViewBag.Title = "Home Page";
}
<h3><b>HTTPPost Method</b></h3>
@using (Html.BeginForm("Submit", "Index", FormMethod.Post))
{
<table>
<tr>
<td>Enter a Number: </td>
<td>@Html.TextBox("Num")</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"></td>
</tr>
</table>
}
<h4 style="color:purple">
<div class="panel-body">
<div class="col-md-12" style="margin-top: 15px;">
<table class="table table-bordered table-responsive table-hover">
<tr>
<th>Input Numbers </th>
</tr>
@foreach (var element in Model)
{
<td>@d.Number</td>
}
</table>
</div>
</div>
</h4>
你能告诉我我的代码有什么问题吗?再次感谢您的帮助。
在您的索引函数中,您需要填充模型并传递给视图。像
Public ActionResult Index()
{
var myList = new List<example>();
return view(myList)
}
在您看来:
@model List<example>
这就是填充索引视图模型的内容。如果您向我们展示返回索引视图的控制器函数,将会有所帮助。
你应该在你的视图中首先写上模型的类型
@model List<ModelMath>
要显示视图,您需要 [HttpGet] 属性 action
[HttpGet]
Public ActionResult Index()
{
//var mathList= new list<ModelMath>();
return view(mathList)
}
我是 MVC 的新手,我正在尝试在 MVC 视图中填充项目列表 class,但是在启动期间模型对象在 .cshtml 文件中为 null。
@foreach (var element in Model)
感谢您的帮助。
我的代码:
public class HomeController : Controller
{
List<ModelMath> mathList = new List<ModelMath>();
[HttpPost]
public ActionResult Submit(FormCollection fc)
{
mathList = new List<ModelMath>();
int num = Convert.ToInt32(fc["Num"]);
while(num > 1)
{
ModelMath modelMath = new ModelMath();
modelMath.Number = num;
mathList.Add(modelMath);
num--;
}
return View(mathList);
}
}
型号class:
public class ModelMath
{
public int Number { get; set; }
}
Index.cshtml
@{
ViewBag.Title = "Home Page";
}
<h3><b>HTTPPost Method</b></h3>
@using (Html.BeginForm("Submit", "Index", FormMethod.Post))
{
<table>
<tr>
<td>Enter a Number: </td>
<td>@Html.TextBox("Num")</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"></td>
</tr>
</table>
}
<h4 style="color:purple">
<div class="panel-body">
<div class="col-md-12" style="margin-top: 15px;">
<table class="table table-bordered table-responsive table-hover">
<tr>
<th>Input Numbers </th>
</tr>
@foreach (var element in Model)
{
<td>@d.Number</td>
}
</table>
</div>
</div>
</h4>
你能告诉我我的代码有什么问题吗?再次感谢您的帮助。
在您的索引函数中,您需要填充模型并传递给视图。像
Public ActionResult Index()
{
var myList = new List<example>();
return view(myList)
}
在您看来:
@model List<example>
这就是填充索引视图模型的内容。如果您向我们展示返回索引视图的控制器函数,将会有所帮助。
你应该在你的视图中首先写上模型的类型
@model List<ModelMath>
要显示视图,您需要 [HttpGet] 属性 action
[HttpGet]
Public ActionResult Index()
{
//var mathList= new list<ModelMath>();
return view(mathList)
}