1

In ASP.NET MVC 3, how can I do something like that?

public JsonResult Create<T>(T field) where T : Field
{
    ...
}

Thanks.

2 Answers 2

1

You can have a gerneric Controller.

public abstract class BaseFieldController<T> : Controller where T : Field
{
    public virtual JsonResult Create(T field)
    {
        ...
    }
}

Then extend from it

public class FieldController : BaseFieldController<Field>
{

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

3 Comments

Umm, and how would its Url look like?
I can't extend it. Is there anyway to somehow do this in routing? Such as /BaseFieldController/GenericName/Create?
Can you explain why you can't extend your controller from another controller?
0
public class SpecificField : Field
{
    ...
}

public class SpecificController : BaseController<SpecificField>
{
    ....
}

public class BaseController<T> : Controller where T : Field
{
    public JsonResult Create( T field )
    {
        ....
    }
}

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.