Reputation: 466
In a controller I set the value:
ViewBag.incremento = 5;
I have the following View:
@model IEnumerable<Gestor.Models.PlanejVenda>
@{
ViewBag.Title = "Índice";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Índice</h2>
<p>
@Html.ActionLink("Criar novo planejamento de compra", "Create")
</p>
<div class="row">
<div class="col-md-6">
@using (Html.BeginForm("Search", "PlanejVendas", FormMethod.Post))
{
<p>
Código: @Html.TextBox("search")
<input type="submit" value="Procurar" />
</p>
}
</div>
<div class="col-md-6">
@using (Html.BeginForm("Search", "PlanejVendas", FormMethod.Post))
{
<p>
Incremento Global: @Html.TextBox("search", new {@ViewBag.incremento })
<input type="submit" value="Confirmar" />
</p>
}
</div>
</div>
<table class="table table-hover">
<tr>
Regula table here showing Ok
I was expecting that I would have the clean value 5 as set by the controler inside the textbox. But It shows: "{ incremento = 5 }" instead
How can I get the value of the ViewBag only, in the example just the number 5?
Upvotes: 0
Views: 110
Reputation: 46219
You can try.
@Html.TextBox("search", (string)@ViewBag.incremento)
instead of
@Html.TextBox("search", new { @ViewBag.incremento })
Upvotes: 1
Reputation: 5550
Separate the ViewBag
from the TextBox
. ViewBag
returns a collection of objects which you need to cast to the correct type.
For testing ignore ViewBag
and use a constant.
What new
is doing is to create an anonymous type instead of a value. Try
@Html.TextBox("search", "5")
If that works then cast the ViewBag item to a string as the answer from D-Shih suggests..
Upvotes: 1
Reputation: 177
Try this:
Incremento Global: @Html.TextBox("search", new { @value= ViewBag.incremento })
Upvotes: 0