Asp.net mvc 视图未将ViewModel发送到控制器
在forAsp.net mvc 视图未将ViewModel发送到控制器,asp.net-mvc,Asp.net Mvc,在for循环中收集数据以传递到方法后,我试图将带有IList的ViewModel从视图发送到控制器,但是,传递的ViewModel继续为null。没有将ViewModel从视图传递到控制器,我缺少了什么 我的ViewModel是: public class HoleViewModel : IEnumerable { public int FacilityId { get; set; } public int CourseId { get; set; }
循环
中收集数据以传递到方法后,我试图将带有IList
的ViewModel从视图发送到控制器,但是,传递的ViewModel继续为null。没有将ViewModel从视图传递到控制器,我缺少了什么
我的ViewModel是:
public class HoleViewModel : IEnumerable
{
public int FacilityId { get; set; }
public int CourseId { get; set; }
//public Hole Hole { get; set; }
public IList<Hole> Holes { get; set; }
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
公共类HoleViewModel:IEnumerable
{
公共int设施ID{get;set;}
public int CourseId{get;set;}
//公共孔{get;set;}
公共IList孔{get;set;}
公共IEnumerator GetEnumerator()
{
抛出新的NotImplementedException();
}
}
我的看法是:
@using GT_App.Models
@model GT_App.ViewModel.HoleViewModel
....
<form method="post" action="/Hole/Create">
<fieldset>
<div>
@{
var holeCount = 4;
}
<table style="display: inline">
<thead>
<th>Number</th>
<th>Yardage</th>
<th>Par</th>
<th>Hdcp</th>
</thead>
@for (int i = 0; i < holeCount; i++)
{
<tr>
<td>
@Html.TextBoxFor(m => m.Holes[i].Number)
</td>
<td>
@Html.TextBoxFor(m => m.Holes[i].Yardage)
</td>
<td>
@Html.TextBoxFor(model => model.Holes[i].Par)
</td>
<td>
@Html.TextBoxFor(model => model.Holes[i].Handicap)
</td>
</tr>
}
</table>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
</form>
@使用GT\u应用程序模型
@模型GT_App.ViewModel.HoleViewModel
....
@{
var-holeCount=4;
}
数
码数
标准杆数
Hdcp
@for(int i=0;im.Holes[i].Number)
@Html.TextBoxFor(m=>m.Holes[i].码数)
@Html.TextBoxFor(model=>model.Holes[i].Par)
@Html.TextBoxFor(model=>model.Holes[i].disability)
}
我的控制器是:
public ActionResult Create()
{
ViewBag.FacilityId = new SelectList(db.Facilities, "FacilityId", "Name");
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(HoleViewModel holes)
{
if (ModelState.IsValid)
{
for (int i = 0; i < holes.Holes.Count; i++)
{
var item = new Hole();
if (Session["FacilityId"] != null || Convert.ToInt32(Session["FacilityId"]) != 0)
{
item.FacilityId = Convert.ToInt32(Session["FacilityId"]);
}
if (Session["CourseId"] != null || Convert.ToInt32(Session["CourseId"]) != 0)
{
item.CourseId = Convert.ToInt32(Session["CourseId"]);
}
item.Number = Convert.ToInt32(Request.Form["Number" + i]);
item.Yardage = Convert.ToInt32(Request.Form["Yardage" + i]);
item.Par = Convert.ToInt32(Request.Form["Par" + i]);
item.Handicap = Convert.ToInt32(Request.Form["Handicap" + i]);
holes.Holes.Add(item);
}
// itterate thru collection to add individual holes to Entity
foreach (Hole hole in holes)
{
db.Holes.Add(hole);
db.SaveChanges();
}
//return RedirectToAction("Index");
}
ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name", Session["CourseId"]);
//return View(Session["CourseId"]);
return RedirectToAction("Index");
}
public ActionResult Create()
{
ViewBag.FacilityId=新的选择列表(db.Facilities,“FacilityId”,“Name”);
ViewBag.CourseId=新的选择列表(db.Courses,“CourseId”,“Name”);
返回视图();
}
[HttpPost]
[ValidateAntiForgeryToken]
公共行动结果创建(HoleViewModel孔)
{
if(ModelState.IsValid)
{
对于(int i=0;i
如果在第二个创建
操作中出现模型验证错误,则通常会使用提交的模型再次返回视图,然后在网页上显示验证错误
您没有这样做-您正在重定向到索引
操作,而不管模型是否有效。我会在调用SaveChanges
之后立即调用RedirectToAction
,然后在方法的末尾返回视图(holes)代码>
哦,我不会把SaveChanges
调用放在循环中。循环后再做。该代码还有其他问题,但我就到此为止…:-) 您的第一个创建操作不会将模型传递给视图,因此视图将具有空模型。没关系,这很正常。提交表单时是否存在问题?是,错误出现在“创建”按钮上。更具体地说,错误发生在“For”循环上。由于未传入ViewModel,因此for循环无法识别holes.holes.Count()。我无法通过for循环的开头。由于未传入ViewModel,for循环无法识别holes.holes.Count()。