当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);
}
发布于 2016-07-23 02:00:52
你想做这样的事情:
类:
public class ModelClassHere {
public float Liability {get;set;}
}
控制器:
public ActionResult Index(ModelClassHere model) {
model.Liability = 10.00;
return View(model); // pass model to the view
}
查看:
@Html.TextBoxFor(x => x.Liability) // 'x' can be anything
编辑*
如果您已经有一个模型,并且需要传递一个简单的值:
控制器:
public ActionResult Index(ModelClassHere model, string otherValue) {
model.Liability = 10.00;
ViewBag.Liability = model.Liability;
return View(model); // pass model to the view
}
查看:
<input type="text" id="otherValue" name="otherValue" value="@ViewBag.Liability.ToString()" />
发布于 2016-07-23 02:18:38
您可以使用@Html.TextBox("AdjustedLiabilityAmount",(十进制)ViewBag.AdjustedValue)}
或
@Html.TextBox("AdjustedLiabilityAmount",Model.AdjustedLiabilityAmount == null?(十进制)ViewBag.AdjustedValue: Model.AdjustedLiabilityAmount)}
在decimal类型中,您可以输入所需的类型。
发布于 2016-07-23 01:59:10
您需要在控制器中传递模型
return view(myModelName);
确保您有权在您的控制器中访问它。
此外,视图还必须引用顶部@model行中的模型。
最后,要调用该模型,它将是
查看:
Model.myModelName
https://stackoverflow.com/questions/38532604
复制相似问题