0

我遇到了部分视图的问题。我有一个公告的索引视图,我正在尝试添加一个部分视图以在同一页面内创建一个新的公告。

我可以显示部分视图,并提交表单以创建新记录。记录被提交到数据库中,但是在重新渲染页面时,出现错误:执行处理程序'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'的子请求时出错,{“不允许子操作执行重定向操作."} 在我的索引页中的 Html.Action 语句中。

我一直在努力完成这项工作,首先将 Html.Partial 更改为 Html.Action 语句,因为控制器方法没有触发,其次,在我读到这个错误是因为在渲染页面时,. NET 不知道我的重定向操作在做什么,因此会自动停止它,尝试在代码块内将 Html.Action 更改为 Html.RedirectAction,但仍然得到上面详述的相同错误。

我的模型很简单:

public class Announcement
{
    public Announcement()
    {
        AnnouncementDate = System.DateTime.Now;
    }

    [Key]
    public int AnnouncementID { get; set; }

    public string Title { get; set; }
    public string Type { get; set; }

}

我的控制器方法:

public ViewResult Index(string searchString, int? page)
    {
        var Announcements = from a in db.Announcements
                        select a;
        if (!String.IsNullOrEmpty(searchString))
        {
            Announcements = Announcements.Where(s => (s.Title.ToUpper().Contains(searchString.ToUpper()) || s.AnnouncementText.ToUpper().Contains(searchString.ToUpper())));
        }
        Announcements = Announcements.OrderBy(s => s.Title);

        int pageSize = 10;
        int pageNumber = (page ?? 1);

        return View(Announcements.ToPagedList(pageNumber, pageSize));
    }

    //
    // GET: /Announcement/Create

    public ActionResult Create()
    {
        Announcement announcement = new Announcement();

        return PartialView(announcement);       
    }

    //
    // POST: /Announcement/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Announcement announcement)
    {
        if (ModelState.IsValid)
        {
            db.Announcements.Add(announcement);
            db.SaveChanges();
            return RedirectToAction("Index"); 
        } 

        return View(announcement);
    }

索引.cshtml

@model PagedList.IPagedList<Project.Models.Announcement>     
@using PagedList.Mvc;   
@using PagedList;

@using (Html.BeginForm())
{
    @Html.TextBox("SearchString", ViewBag.CurrentFilter as string, new { @class = "search-query", placeholder = "Search by name" })
    <input type="submit" value="Search" class="btn" />
}
@item.Title
@item.Type
@Html.Action("Create"); // This is the line causing errors after I submit the Create form.  Have tried changing to Html.RedirectAction

创建.cshtml:

@model Project.Models.Announcement

@using (Html.BeginForm())   
{
    @Html.AntiForgeryToken()

    @Html.TextBoxFor(model => model.Title, new { @style = "width:250px" })
    @Html.TextBoxFor(model => model.Type, new { @style = "width:250px" })

    <input type="submit" value="Create" class="btn btn-small" />        
}
4

1 回答 1

2

在本地做了一些测试后...

你可以保留

@Html.Action("Create")

然而,你必须改变一件小事。在您的表单中定义 POST 指向的操作:)

@model Project.Models.Announcement

@using (Html.BeginForm("Create"))   
{
    @Html.AntiForgeryToken()

    @Html.TextBoxFor(model => model.Title, new { @style = "width:250px" })
    @Html.TextBoxFor(model => model.Type, new { @style = "width:250px" })

    <input type="submit" value="Create" class="btn btn-small" />        
}
于 2013-06-19T09:45:49.063 回答