简体   繁体   English

ASP.net mvc5在同一视图上有多个动作?

[英]ASP.net mvc5 multple actions on the same view?

I'm trying to have multiple actions accessible from my Index page instead of having a separate view for each. 我正在尝试从“索引”页面访问多个操作,而不是每个操作都有单独的视图。 But I'm not having much luck and I'm pretty much stuck. 但是我运气不太好,我很困。

My BeginForm can't resolve action Create nor can it resolve the controller CategoriesController . 我的BeginForm无法解析动作Create也无法解析控制器CategoriesController

This is my controller: 这是我的控制器:

public class CategoriesController : Controller
{
    private readonly ApplicationDbContext _db = new ApplicationDbContext();

    public async Task<ActionResult> Index()
    {
        return View(await _db.Categories.ToListAsync());
    }

    public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "Id,Name")] Category category)
    {
        if (ModelState.IsValid)
        {
            _db.Categories.Add(category);
            await _db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        return View(category);
    }

    public async Task<ActionResult> Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Category category = await _db.Categories.FindAsync(id);
        if (category == null)
        {
            return HttpNotFound();
        }
        return View(category);
    }

    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> DeleteConfirmed(int id)
    {
        Category category = await _db.Categories.FindAsync(id);
        _db.Categories.Remove(category);
        await _db.SaveChangesAsync();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            _db.Dispose();
        }
        base.Dispose(disposing);
    }
}

And this is my view 这是我的看法

@using MultipleActionsSameView.Controllers
@model IEnumerable<MultipleActionsSameView.Models.Category>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table> 

@using (Html.BeginForm("Create", "CategoriesController", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Category</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

use "Categories" instead of "CategoriesController" 使用“类别”而不是“类别控制器”

 @using (Html.BeginForm("Create", "Categories", FormMethod.Post))
 {
      //your code
 }

sample here: BeginForm 示例在这里: BeginForm

您是否尝试在Html.BeginForm中删除“ CategoriesController”的“ Controller”后缀?

To have multiple submit buttons in one view do the following: 要在一个视图中具有多个提交按钮,请执行以下操作:

Create an attribute... 创建一个属性...

using System;
using System.Reflection;
using System.Web.Mvc;

/// <summary>
/// The MultipleButtonAttribute class is a custom attribute to cater for a view form with multiple submit buttons.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultipleButtonAttribute : ActionNameSelectorAttribute
{
    public string Name { get; set; }
    public string Argument { get; set; }

    /// <summary>Determines whether the action name is valid in the specified controller context.</summary>
    /// <param name="controllerContext">The controller context.</param>
    /// <param name="actionName">The name of the action.</param>
    /// <param name="methodInfo">Information about the action method.</param>
    /// <returns>True if the action name is valid in the specified controller context; otherwise, false.</returns>
    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        var isValidName = false;
        var keyValue = string.Format("{0}:{1}", this.Name, this.Argument);
        var value = controllerContext.Controller.ValueProvider.GetValue(keyValue);

        if (value != null)
        {
            controllerContext.Controller.ControllerContext.RouteData.Values[this.Name] = this.Argument;
            isValidName = true;
        }

        return isValidName;
    }

Then in the view I have multiple submit buttons and a form pointing at the index action like so... 然后在视图中,我有多个提交按钮和一个指向索引操作的表单,如下所示:

@using (Html.BeginForm("Index", "Categories", FormMethod.Get, new { id = "mainForm" }))
{
    <input type="submit" value="Create" name="action:Create" />
    <input type="submit" value="Edit" name="action:Edit" />
}

Then in my controller: 然后在我的控制器中:

[HttpGet]
[MultipleButton(Name = "action", Argument = "Create")]
public ActionResult Create()

[HttpGet]
[MultipleButton(Name = "action", Argument = "Edit")]
public ActionResult Edit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM