【问题标题】:MVC3 Populate 'Create' View for related objectMVC3 为相关对象填充“创建”视图
【发布时间】:2011-02-04 19:25:37
【问题描述】:

相关帖子:Populating a select box in a form using related ID in MVC3

我有一个使用 MVC3 和 ADO.NET Linq-to-Entities 生成的 Listview。

我已经能够创建一个简单的列表视图,使用“创建”和“编辑”操作链接命令来创建“相关对象”。

 @Html.ActionLink("Create Shipping Profile", "../PrintProfile/Create", new { id = item.ProductId }) |
 @Html.ActionLink("Edit Shipping Profile", "../PrintProfile/Edit", new { id = item.ProductId })

当我创建 'PrintProfile' 我希望“创建”视图使用“ProductId”(这是数据库表关系)“预填充”,以便我可以创建配置文件,然后编辑列表中每个项目的关联配置文件。

这是 PrintProfile 的创建控制器

    public ActionResult Create(int ProductId)
    {
        var entities = new CS_ShippingProfiles();
        entities.ProductId = ProductId; // Assign the ProductId I want to 'create'
        return View(entities);
    }

    [HttpPost]
    public ActionResult Create(CS_ShippingProfiles Profile)
    {
        if (ModelState.IsValid)
        {
            var entities = new LabelServiceEntities();
            entities.AddToCS_ShippingProfiles(Profile);
            entities.SaveChanges();

            return Redirect("/Products");
        }
        return View(Profile);
    }

但是当我点击链接时,我得到一个“空”参数异常!我做错了什么?!

【问题讨论】:

    标签: linq-to-entities asp.net-mvc-3 ado.net-entity-data-model


    【解决方案1】:

    Create 操作上的操作参数称为 ProductId,因此请确保生成正确的链接:

    @Html.ActionLink(
        "Create Shipping Profile", 
        "Create", 
        "PrintProfile", 
        new { ProductId = item.ProductId }, 
        null
    ) 
    |
    @Html.ActionLink(
        "Edit Shipping Profile", 
        "Edit", 
        "PrintProfile", 
        new { ProductId = item.ProductId }, 
        null
    )
    

    还要注意如何指定控制器和操作,而不是像您那样使用一些相对 url (../PrintProfile/Create)。

    【讨论】:

    • 好东西!谢谢!像魅力一样工作 - 希望这对未来的人有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 2016-09-03
    相关资源
    最近更新 更多