【发布时间】:2015-08-03 09:31:03
【问题描述】:
我正在尝试使用@Html.RenderAction 在索引视图中显示创建视图,以便将其显示在一个页面中。
用户输入名字、姓氏等,然后单击创建按钮提交数据,然后通过重新加载索引视图(不使用 AJAX)将新记录添加到下表中。
当我单击创建按钮添加新记录时,出现以下错误:
执行处理程序的子请求时出错 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'。
在记录添加到数据库并重定向到索引视图之后的@{ Html.RenderAction("Create"); } 脚本上。
我在这里做错了什么?可以这样使用RenderAction吗?很奇怪,当我点击创建按钮时,它会先点击Index Action,然后再点击Create(Post) Action,为什么不直接点击Action(Post)呢?
代码如下:
索引视图:
@model IEnumerable<MailMerge.ViewModels.EmployeeIndexViewModel>
@{
ViewBag.Title = "Home Page";
}
@{ Html.RenderAction("Create"); }
<h2>List</h2>
<table class="table">
<thead>
<tr>
<th>@Html.DisplayNameFor(m => m.FirstName)</th>
<th>@Html.DisplayNameFor(m => m.LastName)</th>
<th>@Html.DisplayNameFor(m => m.Address)</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
</tr>
}
</tbody>
</table>
创建视图:
@model MailMerge.ViewModels.EmployeeCreateViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</div>
</div>
}
控制器:
public class HomeController : Controller
{
TestEntities db = new TestEntities();
public ActionResult Index()
{
var employees = db.Employees.ToList();
List<EmployeeIndexViewModel> employeesVM = new List<EmployeeIndexViewModel>();
foreach (Employee employee in employees)
{
employeesVM.Add(new EmployeeIndexViewModel
{
ID = employee.ID,
FirstName = employee.FirstName,
LastName = employee.LastName,
Address = employee.Address
});
}
return View(employeesVM);
}
public ActionResult Create()
{
EmployeeCreateViewModel employeeVM = new EmployeeCreateViewModel();
return PartialView("_Create", employeeVM);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(EmployeeCreateViewModel employeeVM)
{
if (ModelState.IsValid)
{
Employee employee = new Employee();
employee.FirstName = employeeVM.FirstName;
employee.LastName = employeeVM.LastName;
employee.Address = employeeVM.Address;
db.Employees.Add(employee);
db.SaveChanges();
return RedirectToAction("Index");
}
return PartialView("_Create", employeeVM);
}
}
【问题讨论】:
-
如果在控制器动作中设置断点会发生什么?
-
@PatrickHofman 控制器动作没有问题,提交数据后出现在索引视图上
-
您可以通过从 Html.Beginform 传递动作和控制器参数来检查,即 @using(Html.BeginForm("Create", "Home"))
标签: c# asp.net-mvc renderaction