C# MVC4 cshtml页面函数调用
我需要在mvc页面上的可编辑文本框中显示一个值,当它第一次加载时(如果存在)。我有一个函数,负责获取所需的值,但我需要传入当前模型的参数,以从数据库中获取所需的值。 我遇到的问题是将此值输入文本框。我尝试的是 cshtml:C# MVC4 cshtml页面函数调用,c#,asp.net-mvc-4,razor,C#,Asp.net Mvc 4,Razor,我需要在mvc页面上的可编辑文本框中显示一个值,当它第一次加载时(如果存在)。我有一个函数,负责获取所需的值,但我需要传入当前模型的参数,以从数据库中获取所需的值。 我遇到的问题是将此值输入文本框。我尝试的是 cshtml: @Html.TextBoxFor(model => model.AdjustedLiabilityAmount, new { @Value=OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjus
@Html.TextBoxFor(model => model.AdjustedLiabilityAmount, new { @Value=OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount))}
@Html.TextBoxFor(model => model.AdjustedLiabilityAmount, new { @Value=ViewBag.AdjustedValue)}
我得到一条红色曲线,表明“Value”名称在当前上下文中不存在
所以我尝试了一种我读到的不同的技术,就像这样
控制器:
public ActionResult Index()
{
ViewBag.AdjustedValue = OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount);
public ActionResult Index(ModelClassHere model) {
model.Liability = 10.00;
return View(model); // pass model to the view
}
public ActionResult Index(ModelClassHere model, string otherValue) {
model.Liability = 10.00;
ViewBag.Liability = model.Liability;
return View(model); // pass model to the view
}
cshtml:
@Html.TextBoxFor(model => model.AdjustedLiabilityAmount, new { @Value=OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount))}
@Html.TextBoxFor(model => model.AdjustedLiabilityAmount, new { @Value=ViewBag.AdjustedValue)}
这一次,我得到了一个红色的扭曲的名称“模型”在当前上下文中不存在
我确信我只是缺少了一些基本的东西,因为我是MVC的新手
非常感谢您的帮助
整个操作结果索引:
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);
}
你需要在考试中通过你的模型 控制器
return view(myModelName);
确保您可以在控制器中访问它
此外,视图还必须引用顶部@model行中的模型
最后,我们要称之为模型
视图:
Model.myModelName
你需要在考试中通过你的模型 控制器
return view(myModelName);
确保您可以在控制器中访问它
此外,视图还必须引用顶部@model行中的模型
最后,我们要称之为模型
视图:
Model.myModelName
您希望执行以下操作: 类别:
public class ModelClassHere {
public float Liability {get;set;}
}
控制器:
public ActionResult Index()
{
ViewBag.AdjustedValue = OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount);
public ActionResult Index(ModelClassHere model) {
model.Liability = 10.00;
return View(model); // pass model to the view
}
public ActionResult Index(ModelClassHere model, string otherValue) {
model.Liability = 10.00;
ViewBag.Liability = model.Liability;
return View(model); // pass model to the view
}
视图:
编辑*
如果您已经有一个模型,并且需要传递一个简单值:
控制器:
public ActionResult Index()
{
ViewBag.AdjustedValue = OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount);
public ActionResult Index(ModelClassHere model) {
model.Liability = 10.00;
return View(model); // pass model to the view
}
public ActionResult Index(ModelClassHere model, string otherValue) {
model.Liability = 10.00;
ViewBag.Liability = model.Liability;
return View(model); // pass model to the view
}
视图:
您希望执行以下操作: 类别:
public class ModelClassHere {
public float Liability {get;set;}
}
控制器:
public ActionResult Index()
{
ViewBag.AdjustedValue = OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount);
public ActionResult Index(ModelClassHere model) {
model.Liability = 10.00;
return View(model); // pass model to the view
}
public ActionResult Index(ModelClassHere model, string otherValue) {
model.Liability = 10.00;
ViewBag.Liability = model.Liability;
return View(model); // pass model to the view
}
视图:
编辑*
如果您已经有一个模型,并且需要传递一个简单值:
控制器:
public ActionResult Index()
{
ViewBag.AdjustedValue = OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount);
public ActionResult Index(ModelClassHere model) {
model.Liability = 10.00;
return View(model); // pass model to the view
}
public ActionResult Index(ModelClassHere model, string otherValue) {
model.Liability = 10.00;
ViewBag.Liability = model.Liability;
return View(model); // pass model to the view
}
视图:
你可以用
@Html.TextBoxAdjustedLiabilityAmount,DecimalViewBag.AdjustedValue}
或
@Html.TextBoxAdjustedLiabilityAmount,Model.AdjustedLiabilityAmount==null?DecimalViewBag.AdjustedValue:Model.AdjustedLiabilityAmount}
在“十进制类型”中,您可以根据需要输入所需的类型。您可以使用
@Html.TextBoxAdjustedLiabilityAmount,DecimalViewBag.AdjustedValue}
或
@Html.TextBoxAdjustedLiabilityAmount,Model.AdjustedLiabilityAmount==null?DecimalViewBag.AdjustedValue:Model.AdjustedLiabilityAmount}
在decimal type中,您可以输入所需的类型。为什么要使用@Value而不是Value?Razor通常将其检测为写入输出,而不是让C代码将其用作特殊名称的转义值。。。此外,Value不是一个特殊的名称-使用@Value代替Value对我来说毫无意义为什么要使用@Value代替Value?Razor通常将其检测为写入输出,而不是让C代码将其用作特殊名称的转义值。。。此外,Value不是一个特殊的名称-使用@Value代替它对我来说毫无意义问题是ActionResult索引已经返回了一些东西。在上面的问题中添加了整个方法。最好的办法是将字段添加到报表模型中。但是看到我所做的编辑,这是我能想到的唯一其他方法,特别是如果您计划在编辑后将数据传递回控制器@Christ问题是ActionResult索引已经返回了一些内容。在上面的问题中添加了整个方法。最好的办法是将字段添加到报表模型中。但是看到我所做的编辑,这是我能想到的唯一其他方法,特别是如果您计划在编辑后将数据传递回控制器@克里斯