2013-12-18 50 views
0

我想創建一個視圖,我需要兩個不同的按鈕,調用兩個不同的控制器方法。但是當我放置並運行它給我錯誤。兩個提交按鈕Orchard CMS

「所需的防僞表單字段」__RequestVerificationToken「不存在。」

查看

@model Course.ViewModels.CourseIndexVM 
@{ 
Script.Require("ShapesBase"); 
Layout.Title = T("Course").ToString(); 
} 

@using(Html.BeginForm("Index", "CourseAdmin", FormMethod.Get)) { 
<fieldset class="bulk-actions"> 
    <label for="search">@T("Search:")</label> 
    @Html.TextBoxFor(m => m.Search.Expression) 
    <button type="submit">@T("Search")</button> 
    <a href="@Url.Action("Index")">@T("Clear")</a>      
</fieldset> 
} 

@using (Html.BeginForm("Create", "CourseAdmin", FormMethod.Post))  
{ 
<fieldset class="bulk-actions"> 
<button type="submit">@T("Create")</button> 
</fieldset> 
} 


<fieldset> 
<table class="items" summary="@T("List of Courses")"> 
    <colgroup> 
     <col id="Col1" /> 
     <col id="Col2" /> 
     <col id="Col3" /> 
     <col id="Col4" /> 
     <col id="Col5" /> 
     <col id="Col6" /> 
    </colgroup> 
    <thead> 
     <tr> 
      <th scope="col">@T("ID")</th> 
      <th scope="col">@T("Name")</th> 
      <th scope="col">@T("Description")</th> 
     </tr> 
    </thead> 
    @foreach (var item in Model.Courses) { 
    <tr> 
     <td>@item.Id</td> 
     <td>@item.Name</td> 
     <td>@item.Description</td> 
     <td> 
      <div> 
       <a href="@Url.Action("Edit", new {item.Id})" title="@T("Edit")">@T("Edit")</a>@T(" | ")      
       <a href="@Url.Action("Delete", new {item.Id, returnUrl = Request.Url.PathAndQuery})">@T("Delete")</a>      
      </div> 
     </td> 
    </tr> 
    } 
</table> 
@Display(Model.Pager) 
</fieldset> 
Controller 

namespace CourseAdmin.Controllers 
{ 
[Admin] 
public class CourseAdminController : Controller, IUpdateModel 
{ 
    private readonly IContentDefinitionService _contentDefinitionService; 
    private readonly IRepository<CoursePartRecord> _coursePartRepository; 
    private readonly IContentManager _contentManager; 
    private readonly IProjectionManager _projectionManager; 
    private readonly INotifier _notifier; 
    private readonly IOrchardServices _orchardService; 

    private dynamic Shape { get; set; } 
    private readonly ISiteService _siteService; 

    bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) { 
     return TryUpdateModel(model, prefix, includeProperties, excludeProperties); 
    } 

    void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) { 
     ModelState.AddModelError(key, errorMessage.Text); 
    } 

    public CourseAdminController(

     IShapeFactory shapeFactory, 
     ISiteService siteService, 
     IContentDefinitionService contentDefinitionService, 
     IContentManager contentManager, 
     IProjectionManager projectionManager, 
     IRepository<CoursePartRecord> coursePartRepository, 
     IOrchardServices orchardService, 
     INotifier notifier) 
    { 
     Shape = shapeFactory; 
     _siteService = siteService; 
     _contentDefinitionService = contentDefinitionService; 
     _contentManager = contentManager; 
     _projectionManager = projectionManager; 
     _coursePartRepository = coursePartRepository; 
     _notifier = notifier; 
     _orchardService = orchardService; 
    } 

    public ActionResult Create() 
    {      
     var course = _orchardService.ContentManager.New("Courses"); 
     var editor = Shape.EditorTemplate(TemplateName: "Parts/Course.Create", Model: new CourseCreateVM(), Prefix: null); 
     editor.Metadata.Position = "2"; 
     dynamic model = _orchardService.ContentManager.BuildEditor(course); 
     model.Content.Add(editor); 

     // Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation. 
     return View((object)model);           
    } 


    [HttpPost, ActionName("Create")] 
    public ActionResult CreatePOST(CourseCreateVM courseModel) 
    { 
     var course = _orchardService.ContentManager.New("Courses"); 
     // Cast the customer to a UserPart 
     var coursePart = course.As<CoursePart>(); 

     coursePart.Name = courseModel.Name; 
     coursePart.Description = courseModel.Description; 

     _orchardService.ContentManager.Create(course); 

     return RedirectToAction("Index"); 
     }    
    } 
} 

回答

0

我認爲你需要使用

@using (Html.BeginFormAntiForgeryPost(...)) { ... } 

的包裝爲表單。