【问题标题】:ASP.NET Controller return variable View accessASP.NET 控制器返回变量视图访问
【发布时间】:2016-05-18 19:22:08
【问题描述】:

我在 asp.net mvc 中创建了以下数据模型:

public class DetailModel
{ 
public string name { get; set; }
public string value { get; set; }
public Highcharts chart { get; set; }
}

在控制器中我积累数据:

public ActionResult detail()
{
var viewModel = new DetailModel();

Highcharts chart = new Highcharts("chart") ....

viewModel.chart = chart;
viewModel.name = "test string";
viewModel.value = "1a"

return View(viewModel);
}

在视图模型中,我想访问 vieModel 数据,但它不起作用。

我已经用下面的代码试过了:

<%: viewModel.value %>

<%: viewModel %>

@(Model)

或者这是将多个值传递给视图的错误方式?

【问题讨论】:

    标签: c# asp.net asp.net-mvc highcharts


    【解决方案1】:

    您需要通过视图顶部的@model 声明告诉视图它应该期待什么样的模型:

    <!-- The fully qualified name may vary -->
    @model YourProject.Models.DetailModel
    

    完成此操作后,您可以使用 Model 引用此模型上存在的任何属性,如下例所示:

    <!-- Referencing the name property -->
    Name: @Model.name 
    <!-- Referencing the name property -->
    Value: @Model.value
    <!-- You can access the properties of your chart as well -->
    @Model.chart.YourPropertyHere
    

    【讨论】:

      【解决方案2】:

      你应该这样确定你的模型

      @model DetailModel
      

      你可以像这样访问你的模型属性

      @Model.name 
      
      @Model.value
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-06
        • 2020-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-25
        相关资源
        最近更新 更多