【问题标题】:ASP.NET MVC redirect actions error with 'create' partial view on 'index' pageASP.NET MVC 重定向操作错误与“索引”页面上的“创建”部分视图
【发布时间】:2013-06-15 18:28:23
【问题描述】:

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

我可以显示部分视图,并提交表单以创建新记录。记录被提交到数据库中,但是在重新渲染页面时,出现错误:执行处理程序'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" />        
}

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

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

    你可以保留

    @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" />        
    }
    

    【讨论】:

    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 2019-05-10
    相关资源
    最近更新 更多