提交数据后 @Html.RenderAction 上的错误 System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper
Error System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper on @Html.RenderAction after submit the data
我正在尝试使用 @Html.RenderAction
在索引视图中显示创建视图,以便在一页中显示它。
用户输入名字、姓氏等,然后单击创建按钮提交数据,然后通过重新加载索引视图(没有 AJAX).
当我单击“创建”按钮添加新记录时,出现以下错误:
Error executing child request for handler
'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);
}
}
即使您 post 从您的部分视图中查看,它也在索引视图内部和它的路径下。如果您想 post 到不同的视图,您需要在提交时将表单操作设置为 post 到该视图。
<form method="post" action="@Url.Action("Create", "Home")" >
您可以在 Html.Beginform()
中添加操作和控制器参数
@using(Html.BeginForm("Create", "Home"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
....
</div>
}
我正在尝试使用 @Html.RenderAction
在索引视图中显示创建视图,以便在一页中显示它。
用户输入名字、姓氏等,然后单击创建按钮提交数据,然后通过重新加载索引视图(没有 AJAX).
当我单击“创建”按钮添加新记录时,出现以下错误:
Error executing child request for handler '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);
}
}
即使您 post 从您的部分视图中查看,它也在索引视图内部和它的路径下。如果您想 post 到不同的视图,您需要在提交时将表单操作设置为 post 到该视图。
<form method="post" action="@Url.Action("Create", "Home")" >
您可以在 Html.Beginform()
中添加操作和控制器参数@using(Html.BeginForm("Create", "Home"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
....
</div>
}