Question

I am trying to create a view where i need to two different button calling two different controller methods. but when i place and run it gives me error.

"The required anti-forgery form field "__RequestVerificationToken" is not present."

View

@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");
        }             
    }
}
Était-ce utile?

La solution

I think you need to use

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

as the wrapper for your form.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top