如何从以ASP.Net MVC模式通过枚举 [英] How to pass Enum from view to model ASP.Net MVC

查看:109
本文介绍了如何从以ASP.Net MVC模式通过枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

控制器code看起来像

the controller code looks like

public class EmployeeController : Controller
{
    public enum EmployeeType
    {
        RecruitmentOffice,
        ResearchInstitute
    }

    public ActionResult Details(int id, EmployeeType type)
    {            
        switch (type)
        {
            case EmployeeType.RecruitmentOffice:
                // load repository
                // load domain object
                // load view specific to recruitment office
                break;
            case EmployeeType.ResearchInstitute:
                // load repository
                // load domain object
                // load view specific to recruitment office
                break;
        }
    }
}

现在我想,如何生成表格操作方法这将指向详细信息操作方法,并通过枚举价值如 EmployeeType.RecruitmentOffice或EmployeeType.ResearchInstitute

now i want that how to generate form action method which will point to Details action method and pass the enum value like EmployeeType.RecruitmentOffice or EmployeeType.ResearchInstitute

和当我再次将调用该操作方法,通过的jQuery 然后我怎么能传递参数中的id和放大器; EmployeeType

and again when i will call that action method by jquery then how could i pass parameter for id & EmployeeType.

请与样品code讨论。谢谢

please discuss with sample code. thanks

推荐答案

有关发送为串什么并转换为枚举

What about sending as string and convert to enum

public ActionResult Details(int id, string type)
{ 
EmployeeType empType= (EmployeeType) Enum.Parse(
                                          typeof(EmployeeType), type, true );
}

或做写自定义模型粘合剂。

or do write the custom model binder.

注意:请求参数是字符串

Note: Request params are string

这篇关于如何从以ASP.Net MVC模式通过枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆