显示要查看的对象列表和 return 值
Display a list of objects and return values to view
我有 2 个模型。在第一个模型 "Checkmodel" 中描述了对象的外观。
第二个模型 "Checkmodels" 基于 "Checkmodel" 创建对象列表。
现在我尝试在我的视图中显示它,但我不知道该怎么做。
- 型号"Checkmodel"
namespace WebAppMVCtests.Models
{
public class CheckModel
{
public string Name { get; set; }
public bool IsChecked { get; set; }
public CheckModel(string name, bool ischecked)
{
this.Name = name;
this.IsChecked = ischecked;
}
}
}
- 型号"Checkmodels"
namespace WebAppMVCtests.Models {
public class CheckModels : List<CheckModel>
{
private List<CheckModel> modellist;
public CheckModels()
{
modellist = new List<CheckModel>();
}
public void Add(string name, bool ischecked)
{
CheckModel newModel = new CheckModel(name, ischecked);
this.Add(newModel);
}
}
}
控制器
namespace WebAppMVCtests.Controllers
{
public class HomeController : Controller
{
// GET: Home
[HttpGet]
public ActionResult Index()
{
CheckModels chmdls = new CheckModels();
List<string> e = new List<string>();
e.Add("John");
e.Add("Kevin");
e.Add("Mark");
string name = "";
bool ischecked = false;
foreach (var dir in e)
{
CheckModel newCheckModel = new CheckModel(name, ischecked);
chmdls.Add(newCheckModel);
}
return View();
}
[HttpPost]
public ActionResult Index(List<CheckModel> list)
{
return View();
}
}
}
查看
@model WebAppMVCtests.Models.CheckModels
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
for (var i = 0; i < Model.Count(); i++)
{
<table>
<tr>
<td>
@Html.DisplayFor(it => it[i].Name)
</td>
<td>
@Html.CheckBoxFor(it => it[i].IsChecked)
</td>
</tr>
</table>
}
<input id="Submit1" type="submit" value="submit" />
}
在您的 Index 方法中执行此操作:
return View(chmdls);
我有 2 个模型。在第一个模型 "Checkmodel" 中描述了对象的外观。 第二个模型 "Checkmodels" 基于 "Checkmodel" 创建对象列表。
现在我尝试在我的视图中显示它,但我不知道该怎么做。
- 型号"Checkmodel"
namespace WebAppMVCtests.Models { public class CheckModel { public string Name { get; set; } public bool IsChecked { get; set; } public CheckModel(string name, bool ischecked) { this.Name = name; this.IsChecked = ischecked; } } }
- 型号"Checkmodels"
namespace WebAppMVCtests.Models { public class CheckModels : List<CheckModel> { private List<CheckModel> modellist; public CheckModels() { modellist = new List<CheckModel>(); } public void Add(string name, bool ischecked) { CheckModel newModel = new CheckModel(name, ischecked); this.Add(newModel); } } }
控制器
namespace WebAppMVCtests.Controllers
{
public class HomeController : Controller
{
// GET: Home
[HttpGet]
public ActionResult Index()
{
CheckModels chmdls = new CheckModels();
List<string> e = new List<string>();
e.Add("John");
e.Add("Kevin");
e.Add("Mark");
string name = "";
bool ischecked = false;
foreach (var dir in e)
{
CheckModel newCheckModel = new CheckModel(name, ischecked);
chmdls.Add(newCheckModel);
}
return View();
}
[HttpPost]
public ActionResult Index(List<CheckModel> list)
{
return View();
}
}
}
查看
@model WebAppMVCtests.Models.CheckModels
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
for (var i = 0; i < Model.Count(); i++)
{
<table>
<tr>
<td>
@Html.DisplayFor(it => it[i].Name)
</td>
<td>
@Html.CheckBoxFor(it => it[i].IsChecked)
</td>
</tr>
</table>
}
<input id="Submit1" type="submit" value="submit" />
}
在您的 Index 方法中执行此操作:
return View(chmdls);