在控制器中我设置了值:
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}”而不是
如何才能获得ViewBag的值,在示例中只是数字5?
答案 0 :(得分:1)
你可以试试。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>Pixel Art Maker!</title>
<link href="https://fonts.googleapis.com/css?family=Griffy" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h1>Lab: Pixel Art Maker</h1>
<div id='selections'>
<h2>Choose Grid Size</h2>
<form id="sizePicker">
Grid Height:
<input type="number" id="inputHeight" name="height" min="1" value="11">
Grid Width:
<input type="number" id="inputWeight" name="width" min="1" value="11">
<input type="submit">
</form>
<h2>Pick A Color</h2>
<input type="color" id="colorPicker">
</div>
<h2>Design Canvas</h2>
<table id="pixelCanvas">
<tbody id="tableBody">
</tbody>
</table>
<script src="designs.js"></script>
</body>
</html>
而不是
@Html.TextBox("search", (string)@ViewBag.incremento)
答案 1 :(得分:1)
将ViewBag
与TextBox
分开。 ViewBag
返回需要转换为正确类型的对象集合。
测试忽略ViewBag
并使用常量。
new
正在做的是创建匿名类型而不是值。尝试
@Html.TextBox("search", "5")
如果有效,那么将ViewBag项目转换为字符串,因为D-Shih的答案表明......
答案 2 :(得分:0)
试试这个:
Incremento Global: @Html.TextBox("search", new { @value= ViewBag.incremento })