获取ViewBag的干净值

问题描述 投票:0回答:3

在控制器中我设置了值:

ViewBag.incremento = 5;

我有以下视图:

@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

我期待我将拥有文本框内控制器设置的清理值5。但它显示:“{incremento = 5}”而不是

Showing more then the Viewbag

如何才能获得ViewBag的值,在示例中只是数字5?

asp.net asp.net-mvc razor
3个回答
1
投票

ViewBagTextBox分开。 ViewBag返回您需要转换为正确类型的对象集合。

为了测试忽略ViewBag并使用常量。

new正在做的是创建匿名类型而不是值。尝试

@Html.TextBox("search", "5")

如果有效,那么将ViewBag项目转换为字符串,因为D-Shih的答案暗示......


1
投票

你可以试试。

@Html.TextBox("search", (string)@ViewBag.incremento)

代替

@Html.TextBox("search", new { @ViewBag.incremento })

0
投票

试试这个:

Incremento Global: @Html.TextBox("search", new { @value= ViewBag.incremento })

© www.soinside.com 2019 - 2024. All rights reserved.