【发布时间】:2016-07-22 17:52:46
【问题描述】:
当它第一次加载时,我需要在我的 mvc 页面上的可编辑文本框中显示一个值(如果存在)。我有一个函数可以获取我需要的值,但是我需要从当前模型中传递参数以从数据库中获取我需要的值。 我遇到的问题是将这个值放入文本框中。我尝试的是
cshtml:
@Html.TextBoxFor(model => model.AdjustedLiabilityAmount, new { @Value=OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount))}
我得到一个红色波浪线“当前上下文中不存在名称'Value'”
所以我尝试了一种我读到的不同的技术,就像这样。
控制器:
public ActionResult Index()
{
ViewBag.AdjustedValue = OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount);
cshtml:
@Html.TextBoxFor(model => model.AdjustedLiabilityAmount, new { @Value=ViewBag.AdjustedValue)}
这次我得到了红色的波浪状“名称'模型'在当前上下文中不存在。”
我确定我只是缺少一些基本的东西,因为我是 MVC 的新手。
非常感谢任何帮助。
整个 ActionResult 索引:
public ActionResult Index()
{
ViewBag.AdjustedValue = OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount);
var Report = new OBS_LIB.DTO.JeopardyAssessmentReport();
Report.Stage = 1;
Report.Status = "Active";
Report.ReportItems = OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetJAReportItems(Report.Stage, Report.Status);
return View(Report);
}
【问题讨论】:
-
为什么要使用
@Value而不是Value? Razor 通常将其检测为“写入输出”,而不是让 C# 代码将其用作特殊名称的转义值...(另外,Value不是特殊名称 - 使用@Value对我来说毫无意义)跨度>
标签: c# asp.net-mvc-4 razor