Skip to main content
 首页 » 编程设计

c#之ASP.NET 在引号内使用 ViewBag

2024年02月05日17davidwang456

我正在尝试在 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?

请您参考如下方法:

ViewBag 没有什么特别之处,与其他属性一样,它是在页面上下文中可用的属性。就像在任何其他 C# 代码中一样,在构建字符串时只需引用它:

"Index?community=" + ViewBag.community