【发布时间】:2015-04-09 03:22:18
【问题描述】:
我在编辑页面中使用了模态,这是我的代码:
在视图中
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Edit
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Edit Event Information</h4>
</div>
<div class="modal-body">
@Html.Action("Edit", "Admin",new { id = Model.events_info_id,@class = "btn btn-warning"})
</div>
</div>
</div>
</div>
此控制器用于获取要在模态中显示的信息:
[ChildActionOnly]
public ActionResult Edit(int id = 0)
{
Events_Info_tbl events_info_tbl = db.Events_Info_tbl.Find(id);
if (events_info_tbl == null)
{
return HttpNotFound();
}
return PartialView(events_info_tbl);
}
这是Modal内容的视图:
@model Online_Ballot.Models.Events_Info_tbl
<script src="~/Scripts/Datepicker.js"></script>
<div class="modal-body" style="color:green">
<h2>Edit Events</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.events_info_id)
<div class="form-group">
<label for="usr">Event Name:</label>
@Html.EditorFor(model => model.events_name, new { @class="form-control"})
@Html.ValidationMessageFor(model => model.events_name)
</div>
<div class="form-group">
<label for="usr">Votation Date:</label>
@Html.EditorFor(model => model.events_votation_date, new { @id="voters_bdate"})
@Html.ValidationMessageFor(model => model.events_votation_date)
</div>
<div class="form-group">
<label for="usr">Votation Place:</label>
@Html.EditorFor(model => model.events_place, new { @class="form-control"})
@Html.ValidationMessageFor(model => model.events_place)
</div>
<div class="form-group">
<label for="comment">Event Description:</label>
@Html.TextAreaFor(model => model.events_desc)
</div>
<div class="form-group">
<label for="usr">Is active:</label>
@Html.EditorFor(model => model.is_active, new { @class="form-control"})
@Html.ValidationMessageFor(model => model.is_active)
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
}
此控制器将更新数据:
// POST: /Admin/Edit/5
[ValidateAntiForgeryToken]
public ActionResult Edit(Events_Info_tbl events_info_tbl)
{
if (ModelState.IsValid)
{
db.Entry(events_info_tbl).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(events_info_tbl);
}
当我尝试运行这段代码时,我得到了这个错误:
不允许子动作执行重定向动作。
但是,它更新了数据,我猜它不允许调用RedirectToAction 函数,但我需要。我将如何解决这个问题?
【问题讨论】:
-
“编辑”子操作驻留在哪个控制器上?
-
控制器下方的视图编辑
-
不确定你的意思。控制器名称是什么?
@Html.Action("Edit", "Admin" ...})好像错了,应该指向这个public ActionResult Edit(int id = 0)而不是这个public ActionResult Edit(Events_Info_tbl events_info_tbl) -
是的,我就是这个意思
-
请看我更新的答案。
标签: c# asp.net asp.net-mvc