将值从viewbag传递到视图

时间:2018-03-08 13:54:58

标签: c# asp.net-mvc razor

我试图将视图包中的值传递给视图创建。

控制器

public ActionResult Create()
{
    ViewBag.BanhoTosaId = new SelectList(db.BanhoTosas, "BanhoTosaId", "Tipo");


    if (ViewBag.BanhoTosaId.SelectedValue != null)
    {
        BanhoTosa BT = db.BanhoTosas.Find(ViewBag.BanhoTosaId);
        decimal valorSoma = BT.Valor + 10;
        ViewBag.Total = valorSoma;
    }
    else
    {
        ViewBag.Total = 0;
    }

    return View();
}

视图

<div class="form-group">
    @Html.LabelFor(model => model.Total, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Total, new { htmlAttributes = new { @class = "form-control", value = "@ViewBag.Total" } })
        @Html.ValidationMessageFor(model => model.Total, "", new { @class = "text-danger" })
    </div>
</div>

如果BT将获取BanhoTosa表中的值并添加10,然后将返回viewbag中的总和值。总计。

但是在视图中,@ Html.EditorFor没有收到任何内容

如何让编辑器从viewbag.total接收值?

感谢盖伊的

0 个答案:

没有答案