如何使用带有ID的ActionLink访问具有不同控制器的Action方法的一个控制器ActionMethod

时间:2016-06-03 08:30:37

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

我正在开发一个包含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 Controller 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 CreateMethod

[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);

RouteConfig文件

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&#34; Create&#34;中访问rawMaterials表单上的供应商列表。 ActionMethod我正在创建新的采购订单,然后我被重定向到PurchaseOrders&#34; Index&#34; ActionMethod我可以看到所有PO的列表。现在PO索引视图页面我已经在列表中的每一行添加了链接@Html.ActionLink("Add RawMaterials","Create","RawMaterial",{id = item.PurchaseOrderID},null)

当用户点击PurchaseOrder&#34; s&#34; Index&#34;上的此链接时页面应该转到RawMaterial控制器的HttpGet&#34; POMaterialsCreate&#34;具有PO的id的ActionMethod,另一方面将其作为参数传递给HttpGet&#34; POMaterialsCreate&#34;方法

如何在RawMaterial HttpGet和HttpPost&#34; POMaterialsCreate&#34;中处理此Id。方法,以便我可以针对单个PurchaseOrderID创建多个RawMaterials?如何在RM创建视图中显示此PO ID?

1 个答案:

答案 0 :(得分:0)

我的意思是你的链接应该是:@ Html.ActionLink(“添加RawMaterials”,“创建”,“POMaterialsCreate”,{id = PurchaseOrderID},null)

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