【问题标题】:.NET Razor Model Null.NET Razor 模型空
【发布时间】:2013-04-24 15:54:48
【问题描述】:

我正在使用 MVC 4 处理 .NET 框架。我有一个控制器:

using DataProvider.Queries;
using DataProvider.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ATSGlobalDashboard.Controllers
{
    public partial class ATSNavigatorController : Controller
    {

        public virtual ActionResult Index(DataProvider.Models.GaugeAveragesViewModel model)
        {
            return View(model);
        }
    }
}

一个模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataProvider.Models
{
    public class GaugeAveragesViewModel
    {
        public decimal? timetoresolutiontotalqtd { get; set; }
        public int? backlogtotalcount { get; set; }
        public double? AverageSatisfacationResult { get; set; }
    }
}

然后我想将其用于我的视图/数据视觉效果中的值。我的部分观点是这样设置的:

@model DataProvider.Models.GaugeAveragesViewModel
@{
    ViewBag.Title = "GlobalDashboardModal";
}

<div class="row">
    <div id="satisfactionChartButton" class="large-3 large-offset-1 columns modalGaugeArea allGaugesSetup">
        <div class="chartModalTitle">Customer Satisfaction</div>
        <div id="customerSatisfactionGauge" class="gaugeContainer"></div>
        <input id="customerSatisfactionGaugeValue" value="@Model.AverageSatisfacationResult"/>
        <div class="chartModalDialog"></div>
        <div id="satisfactionOverlay">
            <div class="overlayChartTitle">Customer Satisfaction<br />
                Chart
            </div>
            <img class="overlayImages" src="@Url.Content(Links.Content.img.wht_Desktop_Analytics_png)" />
            <div class="overlayChartDialog">Click to View Chart</div>
        </div>
    </div>
</div>

我希望能够获取输入值并将其与其他一些资源一起使用,但是我收到了 @Model.AverageSatisfactionResult 的空对象错误,无论如何要快速测试一下,还是我在使用中留下了一步@Model 剃须刀语法?

【问题讨论】:

  • 您能告诉我们您的控制器代码吗?控制器是您需要获取实例并将其传递到视图的地方。 @Modeldeclaration 只是为了让您可以在 razorview 中使用 IntelliSense。它不进行任何数据获取
  • 我在控制器部分添加了。谢谢。
  • Index 需要一个 Get ActionResult 方法和一个 Post ActionResult 方法。现在在您的控制器中,当您的页面正在获取时,您的模型将为空,因此您将一个空对象传递给您的视图。这就是你想要的吗?
  • 这不是我想做的,我是 .NET 的新手,并被告知像这样传递模型是一种研究方法。

标签: razor asp.net-mvc-4 view model


【解决方案1】:

如果您将模型传递给控制器​​中的视图,那么您可以使用以下内容

     @model.AverageSatisfacationResult


    <input id="customerSatisfactionGaugeValue"  value="@Model.AverageSatisfacationResult"/>

Replace by

     @Html.EditorFor(m=>model.AverageSatisfacationResult)

@model 不是@Model

已编辑:

我看到了您的控制器,这就是问题的原因。您将模型作为 get Action 的参数,它来自哪里。如果您知道它来自哪里,您可以执行以下操作

 public virtual ActionResult Index()
        {

           DataProvider.Models.GaugeAveragesViewModel model= new GaugeAveragesViewModel{
                    timetoresolutiontotalqtd =1.3,
                    backlogtotalcount=3,
                    AverageSatisfacationResult=5d
                     }
            return View(model);
        }

【讨论】:

  • 我在上面添加了我的控制器代码,当我使用不同的模型智能感知抛出时;预计我会调查。
  • 我相信我根据我正在查看的一些示例设置了错误。我是 .NET 的新手,对剃刀语法一无所知。有没有办法建议您设置控制器以最好地使用视图中的模型值?
  • 像我上面做的那样用虚拟数据测试它
  • 谢谢,它仍然返回 null,如果我使用小写模型,它会返回解析器错误消息:文件中只允许一个“模型”语句。
  • 感谢您的诚实,我会调查的。
猜你喜欢
  • 2018-06-21
  • 2014-02-18
  • 2021-02-04
  • 2015-03-14
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
相关资源
最近更新 更多