I'm trying to pass value from a view bag to the view create.
Controller
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();
}
In case BT will take the value in the bath table and cough and add another 10, and then will return the value of the sum in the viewbag.Total.
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>
value="@ ViewBag.Total" was supposed to receive the value of the ViewBag.