2013-06-19 54 views
0

我遇到部分視圖的問題。我有一個公告的索引視圖,我試圖添加一個局部視圖來在同一頁面內創建一個新的公告。ASP.NET MVC重定向動作錯誤'在'索引'頁上'創建'部分視圖

我可以顯示部分視圖,並提交表單以創建新記錄。記錄被提交到數據庫中,但在重新呈現頁面時,出現錯誤:執行處理程序'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); 
    } 

Index.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 

Create.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" />   
} 

回答

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" />   
} 
+0

Perfet,謝謝! – Evonet

+0

我的下一個問題是現在索引頁面上的搜索表單發佈到我的創建頁面 - 你能夠幫助http://stackoverflow.com/questions/17390588/asp-net-index-page-with-search -posting - 進入 - 局部視圖? – Evonet

相關問題