asp.net-mvc Url.Action 如何从模型中添加参数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19393182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:13:17  来源:igfitidea点击:

Url.Action How to add parameter value from the model

asp.net-mvcasp.net-mvc-4razorurl-action

提问by Branislav

In controller I have the action "GetPhoto":

在控制器中,我有操作“GetPhoto”:

public FileResult GetPhoto(int id)
{
    ...
}

Also, I have Razor code where I'am trying to dynamically add ID parameter from the model:

另外,我有 Razor 代码,我试图在其中动态添加模型中的 ID 参数:

@model ISPIS.Models.KodFazeBiljke
...
<img src="@Url.Action("GetPhoto", new { id = model.KodFazeBiljkeId })" alt="" width="250" height="190"/>

However, it's not possible to write "id = model.KodFazeBiljkeId" because, model does not exist in the current context.

但是,无法编写“id = model.KodFazeBiljkeId”,因为当前上下文中不存在模型。

Any solution? Thanks!

有什么解决办法吗?谢谢!

回答by McGarnagle

Your approach should work -- just have to refer to the model with the upper-case Model:

您的方法应该有效 - 只需使用大写引用模型Model

<img src='@Url.Action("GetPhoto", new { id = Model.KodFazeBiljkeId })' alt="" width="250" height="190"/>