c# - MVC4 cshtml页面函数调用

标签 c# asp.net-mvc-4 razor

我需要在第一次加载时在我的 mvc 页面上的可编辑文本框中显示一个值(如果存在)。我有一个函数负责获取我需要的值,但我需要从当前模型传入参数以从数据库中获取我需要的值。 我遇到的问题是将此值输入文本框。我试过的是

cshtml:

@Html.TextBoxFor(model => model.AdjustedLiabilityAmount, new { @Value=OBS_LIB.BLL.JeopardyAssessment.JeopardyAssessment.GetLatestAdjustedLiabilityAmount(Model.DOTNumber, Model.LiabilityAmount))}

我收到一条红色波浪线,提示“当前上下文中不存在名称‘Value’”

所以我尝试了一种不同的技术,我读到的是这样的。

Controller :

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)}

这次我收到红色波浪形的“名称‘Model’在当前上下文中不存在。”

由于我是 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);
    }

最佳答案

你想做这样的事情:

类:

public class ModelClassHere {
     public float Liability {get;set;}
}

Controller :

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

编辑*

如果您已经有一个模型并且需要传递一个简单的值:

Controller :

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()" />

关于c# - MVC4 cshtml页面函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38532604/

相关文章:

c# - 具有完整属性的私有(private)字段初始化

c# - 等效于 Objective C BitConverter

c# - ASP.Net MVC 在模型错误中定义 PageLink

c# - 在 C# 中使用带有数字节点的 JSON.decode razor webhelper?

RazorGenerator.MsBuild - 实现从开发人员机器到 Buildserver 的和谐

asp.net-mvc - 输出nbsp;通过变量在 Razor ?

c# - 如何只得到奇数参数

c# - WPF 数据网格 : How do you get the content of a single cell?

c# - 使用 Asp.Net MVC 应用程序设置 IdentityServer

html - 如何在确认弹出按钮中禁用 ng-click 事件