两个提交按钮Orchard CMS

时间:2013-12-18 07:19:42

标签: orchardcms orchardcms-1.7

我正在尝试创建一个视图,我需要两个不同的按钮调用两个不同的控制器方法。但是当我放置并运行时它给了我错误。

“所需的防伪表格字段”__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");
        }             
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你需要使用

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

作为表单的包装。