【问题标题】:How to access one controller ActionMethod with different controller's Action method using ActionLink with ID如何使用带有 ID 的 ActionLink 使用不同控制器的 Action 方法访问一个控制器 ActionMethod
【发布时间】:2016-06-03 08:30:36
【问题描述】:

我正在开发一个包含 3 个模型和 3 个控制器(RawMaterial、Supplier 和 PurchaseOrder)的应用程序。这是我的课程。

public class RawMaterial
{
    public int RawMAterialID { get; set; }
    public string RawMaterialName { get; set; }
    public int Quantity { get; set; }

    public int SupplierID { get; set; }
    public virtual Supplier Supplier { get; set; }

    public int PurchaseOrderID { get; set; }
    public virtual PurchaseOrder PurchaseOrder { get; set; }
}

public class Supplier
{
    public int SupplierID { get; set; }
    public string SupplierName { get; set; }
    [DataType(DataType.MultilineText)]
    public string SupplierAddress { get; set; }


    public virtual List<RawMaterial> RawMaterials { get; set; }
}

public class PurchaseOrder
{
    public int PurchaseOrderID { get; set; }

    [DataType(DataType.Date)]
    public DateTime Date { get; set; }

    public virtual List<RawMaterial> RawMaterial { get; set; }
}

PO 的索引视图

<td>
        @Html.ActionLink("Add RawMaterials", "POMaterialsCreate", "RawMaterial", new { id = item.PurchaseOrderID }, null) |
        @Html.ActionLink("Edit", "Edit", new { id=item.PurchaseOrderID }) |
        @Html.ActionLink("Details", "Details", new { id=item.PurchaseOrderID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.PurchaseOrderID })
    </td>

RawMaterials 控制器 HTTPGET CreateMethod

public ActionResult POMaterialsCreate(int id)
    {
        var POrawMaterials = db.RawMaterials.SingleOrDefault(s => s.PurchaseOrderID == id);

        ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "SupplierName");
        return View(POrawMaterials);
    }

RM HTTPPOST 创建方法

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult POMaterialsCreate(RawMaterial rawMaterial)
    {
        if (ModelState.IsValid)
        {
            db.RawMaterials.Add(rawMaterial);
            db.SaveChanges();
            return RedirectToAction("POMaterials");
        }


        ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "SupplierName", rawMaterial.SupplierID);
        return View(rawMaterial);

路由配置文件

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

我正在 ddl.On PurchaseOrders "Create" ActionMethod 中访问 rawMaterials 表单上的供应商列表我正在创建新的采购订单,然后我被重定向到 PurchaseOrders "Index" ActionMethod 在那里我可以看到所有 PO 的列表。现在在POs Index 视图页面我已经在列表中的每一行添加了链接@Html.ActionLink("Add RawMaterials","Create","RawMaterial",{id = item.PurchaseOrderID},null)

当用户在 PurchaseOrder 的“索引”页面上单击此链接时,它应该使用 PO 的 id 转到 RawMaterial 控制器的 HttpGet“POMaterialsCreate”ActionMethod,另一方面将其作为参数传递给 HttpGet“POMaterialsCreate”方法。

如何在 RawMaterial HttpGet 和 HttpPost "POMaterialsCreate" 方法中处理此 ID,以便可以针对单个 PurchaseOrderID 创建多个 RawMaterials?以及如何在 RM create View 中显示此 PO ID?

【问题讨论】:

    标签: c# sql asp.net-mvc linq


    【解决方案1】:

    我的意思是您的链接应该是:@Html.ActionLink("Add RawMaterials","Create","POMaterialsCreate",{id=PurchaseOrderID},null)

    还可以查看浏览器中的调试器控制台

    【讨论】:

    • 这只是一个文本。请检查 RM 的“POMaterialsCreate”方法及其 {code} 并告诉我哪里做错了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多