9

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;
        }
    }
}

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

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

please discuss with sample code. thanks

1
  • just tell me how the url would look like for details action method? how to pass value for enum from view to action method? Commented Nov 18, 2013 at 10:39

4 Answers 4

16

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

Sign up to request clarification or add additional context in comments.

1 Comment

In later versions, it looks like ASP.NET will do the string parsing for you, so you can use the enum directly in the action parameters.
2

This is also pretty cool:

[Flags]
public enum PersonRole { User, Student, Instructor };

then from your razor view:

<button onclick="onclickDeleteRole('@(PersonRoleEnum.User|PersonRoleEnum.Student)')">

and in your javascript:

function onclickDeleteRole(role) {
    var personId = $('#SelectedPersonId').val();

    $.ajax({
        url: window.getUrl('Person/DeletePersonRole'),
        type: 'POST',
        dataType: 'json',
        data: {
            personId: personId,
            roles: role
        },
        success: function (json) {
            alert('success!')
        },
        error: function (jqXHR, status, error) {
            alert('error')
        }
    });
}

and your controller action:

public JsonResult DeletePersonRole(int personId, PersonRoleEnum roles)
{
    // do business logic here...
    // roles will now have value PersonRoleEnum.User|PersonRoleEnum.Student
    // and you can use roles.HasFlag(PersonRoleEnum.User) to check if that flag is set

    return Json(new {Result = "OK"});
}

EDIT: for readability, you can always use strings in javascript, and MVC will parse those for you, e.g.

$.ajax({
        url: window.getUrl('Person/DeletePersonRole'),
        type: 'POST',
        dataType: 'json',
        data: {
            personId: 1,
            roles: 'Student'
        },
        success: function (json) {
            alert('success!')
        },
        error: function (jqXHR, status, error) {
            alert('error')
        }
    });

Comments

1

If you pass an enum value in your form, it will show up as an int in you controller. I think you have 2 ways to handle this :

  • make the type parameter an int, then cast it to your enum in your action
  • make a specific model binder that looks for an input called type and tries to cast it before getting into your action.

Some links, with sample code, may help you : http://www.codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder http://www.codeproject.com/Articles/551576/ASP-NET-MVC-Model-Binding-and-Data-Annotation

1 Comment

just tell me how the url would look like for details action method? how to pass value for enum from view to action method?
1

If your enum is defined like this:

public enum EmployeeType {
    RecruitmentOffice, //value = 0
    ResearchInstitute //value = 1
}

In your View (*.cshtml) you can pass enum value like this:

var enumVal = 0; @* RecruitmentOffice *@
$.ajax({
    url: '@(Url.Action("Details", "Employee"))',
    type: 'POST',
    dataType: 'json',
    data: {
        id: employeeId ,
        type: enumVal
    }
    success: function (result) {
       @* Handle success *@
    }
}

Where the enumVal is the Enum Value you need. And don't forget to decorate your Details action method with [HttPost].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.