我试图在ASP.NET中使用ViewBag,如下所示:
@using (Html.BeginForm("Index?community=" + @ViewBag.community + "&lot=" + @ViewBag.lot + "",
"UploadFile",
FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<label for="file">Upload File:</label>
<input type="file" name="file" id="file" /><br><br>
<input type="submit" value="Upload File" />
<br><br>
@ViewBag.Message
}
我也尝试了以下内容:
@using (Html.BeginForm("Index?community=@ViewBag.community&lot=@ViewBag.lot",
"UploadFile",
FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<label for="file">Upload File:</label>
<input type="file" name="file" id="file" /><br><br>
<input type="submit" value="Upload File" />
<br><br>
@ViewBag.Message
}
是否可以在引号内使用@ViewBag?
答案 0 :(得分:2)
ViewBag
没有什么特别之处,它是页面上下文中可用的属性,与其他任何属性一样。只需在构建字符串时引用它,就像在任何其他C#代码中一样:
"Index?community=" + ViewBag.community
答案 1 :(得分:2)
当然:只需删除@
,因为您已经定义了您在行的开头写了一个表达式(@using
)。这样做:
@using (Html.BeginForm("Index?community=" + ((string)ViewBag.community) + "&lot=" + ((string)ViewBag.lot),[...]
无论如何,我建议使用视图模型将值从控制器传递到视图,而不是ViewBag。