问题
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" })
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
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.
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!
回答1:
Your code looks fine:
@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.
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);
}
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)
来源:https://stackoverflow.com/questions/11336834/mvc-passing-parameters-from-view-to-popup