MVC-将参数从视图传递到弹出窗口 [英] MVC - Passing Parameters from View to Popup

查看:159
本文介绍了MVC-将参数从视图传递到弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having a problem passing parameters from my view to my popup.

在我看来,我具有以下剃刀代码来呈现动作链接",其中当我单击它时,将出现一个弹出窗口:

In my view, I have the following razor code to render an Action Link wherein when I click it, a pop-up will appear:

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

我不确定这是正确的还是部分new { ProductCode = @Model.ProductCode}是否有意义(请向我解释该部分的作用,大家好).

I'm not quite sure if this is correct or if the part new { ProductCode = @Model.ProductCode} makes any sense (please explain to me what that part does, anybody hihi).

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

Anyway, my pop-up code accepts a parameter like this:

@model MySuperStore.Models.ViewModel.ProductsModel

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

Whenever I try to display the ProductCode via @Mode.ProductCode, I always receive an error saying the reference not set to an instance of an object.

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

I have tried placing the ProductCode in a ViewData through the MainView and accessing it on the pop-up but that doesn't seem to work either.

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

Can somebody please help me? Thanks. Cheers!

推荐答案

您的代码看起来不错:

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

只需确保要放入此代码的视图具有强类型,并且确保呈现该视图的控制器操作已将实际模型传递给它.

Just ensure that the view you are putting this code in is strongly typed and that the controller action that rendered it passed an actual model to it.

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

As far as the Edit action is concerned you should also ensure you are invoking you are passing a non-null model to the view:

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

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

Now inside your Edit.cshtml view (or partial view if you are opening this using jQuery UI dialog or something in a pop-up) you could use the properties of the model:

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

这篇关于MVC-将参数从视图传递到弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆