【问题标题】:MVC - Passing Parameters from View to PopupMVC - 将参数从视图传递到弹出窗口
【发布时间】:2012-07-05 01:15:47
【问题描述】:

我在将参数从视图传递到弹出窗口时遇到问题。

在我看来,我有以下剃须刀代码来呈现一个动作链接,当我点击它时,会出现一个弹出窗口:

@Html.ActionLink("[Edit Product]", "Edit", "Products", new { ProductCode = @Model.ProductCode}, new { @class = "editLink" })

我不太确定这是否正确,或者 new { ProductCode = @Model.ProductCode} 部分是否有意义(请向我解释该部分的作用,任何人你好)。

无论如何,我的弹出代码接受这样的参数:

@model MySuperStore.Models.ViewModel.ProductsModel

每当我尝试通过 @Mode.ProductCode 显示 ProductCode 时,我总是收到一条错误消息,指出引用未设置为对象的实例。

我尝试通过 MainView 将 ProductCode 放在 ViewData 中并在弹出窗口中访问它,但这似乎也不起作用。

有人可以帮帮我吗?谢谢。干杯!

【问题讨论】:

    标签: asp.net-mvc view parameters popup viewdata


    【解决方案1】:

    您的代码看起来不错:

    @Html.ActionLink(
        "[Edit Product]", 
        "Edit", 
        "Products", 
        new { ProductCode = Model.ProductCode }, 
        new { @class = "editLink" }
    )
    

    只需确保您放置此代码的视图是强类型的,并且呈现它的控制器操作将实际模型传递给它。

    Edit 操作而言,您还应该确保您正在调用您正在将非空模型传递给视图:

    public ActionResult Edit(int productCode)
    {
        ProductsModel model = ... fetch your model using the productCode
        return View(model);
    }
    

    现在在您的Edit.cshtml 视图中(或部分视图,如果您使用 jQuery UI 对话框或 弹出窗口 中的某些内容打开此视图),您可以使用模型的属性:

    @model ProductsModel 
    @Html.DisplayFor(x => x.ProductCode)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-14
      相关资源
      最近更新 更多