c# - 将枚举传递给强类型部分 View

标签 c# asp.net-mvc asp.net-mvc-3 partial-views strong-typing

我有一个接收枚举的强类型部分 View :

@model MyEnum 
@{
   Layout = null
}
@if (Model == MyEnum.Value1) {
    //... dosomething
}
@if (Model == MyEnum.Value2) {
    //... do another thing
}

我无法像这样正确调用渲染部分

@{ Html.RenderPartial("MyPartialView", MyEnum.Value2); }

有什么想法吗?

最佳答案

除了您在空布局分配后缺少 ; 之外,我看不出是什么阻止您这样做(正如我所说的那样,无法重现) :

@{
    Layout = null;
}

这是一个完整的工作示例,说明这应该有效。

型号:

public enum MyEnum
{
    Value1,
    Value2
}

Controller :

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

Index.cshtml View :

<div>
@{ Html.RenderPartial("MyPartialView", MyEnum.Value2); }
</div>

MyPartialView.cshtml 部分:

@model MyEnum
@{
    Layout = null;
}
@if (Model == MyEnum.Value1) {
    <div>Value 1 was selected</div>
}
@if (Model == MyEnum.Value2)
{
    <div>Value 2 was selected</div>
}

按照预期在结果 HTML 中输出:

<div>Value 2 was selected</div>

关于c# - 将枚举传递给强类型部分 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5983948/

相关文章:

c# - aspx 文件不包含函数定义,定义在连接的 aspx.cs 文件中

c# - 无法使用最新的 Windows 10 IoT Core 在 RPi3 上播放声音文件 (WAV)

C# linq 查询聚合可为空 boolean 值

asp.net-mvc - MVC Html.ActionLink 未呈现。你能看出我做错了什么吗?

jquery - 将对象列表发布到 MVC 5 Controller

javascript - 在 View 中显示的结果表中,用不同颜色为所有偶数项着色

asp.net-mvc-3 - mvc3中选定的菜单项

c# - 快速设置 DataGridView 行高

asp.net-mvc-3 - 如何在 MVC3/4 中将类属性添加到 Html.BeginForm() 以保留最小重载

c# - 将 null 分配给可为空的 int 时出错 - "The value ' null' 对属性无效”