Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:17:25+00:00 2026-05-29T06:17:25+00:00

In my project I have a controller that allows you to create multiple letters

  • 0

In my project I have a controller that allows you to create multiple letters of different types. All of these letter types are stored in the database, but each letter type has different required fields and different views.

Right now I have a route set up for the following URL: /Letters/Create/{LetterType}. I currently have this mapped to the following controller action:

public ActionResult Create(string LetterType)
{
    var model = new SpecificLetterModel();

    return View(model); 
}

I also have a View called Create.cshtml and an EditorTemplate for my specific letter type. This all works fine right now because I have only implemented one Letter Type. Now I need to go ahead and add the rest but the way I have my action set up it is tied to the specific letter type that I implemented.

Since each Letter has its own model, its own set of validations, and its own view, what is the best way to implement these actions? Since adding new letter types requires coding for the model/validations and creating a view, does it make more sense to have individual controller actions:

 public ActionResult CreateABC(ABCLetterModel model);
 public ActionResult CreateXYZ(XYZLetterModel model);

Or is there a way I can have a single controller action and easily return the correct model/view?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:17:25+00:00Added an answer on May 29, 2026 at 6:17 am

    You can do one of the following:

    Have a different action method for each input. This is because the mvc framework will see the input of the action method, and use the default model binder to easily bind the properties of that type. You could then have a common private method that will do the processing, and return the view.

    Assuming XYZLetterModel and ABCLetterModel are subclasses of some base model, your controller code could look like:

    public class SomeController : Controller
        {
            private ISomeService _SomeService;
    
            public SomeController(ISomeService someService)
            {
                _SomeService = someService;
            }
    
            public ViewResult CreateABC(ABCLetterModel abcLetterModel)
            {
                // this action method exists to allow data binding to figure out the model type easily
                return PostToServiceAndReturnView(abcLetterModel);
            }
    
            public ViewResult CreateXYZ(XYZLetterModel xyzLetterModel)
            {
                // this action method exists to allow data binding to figure out the model type easily
                return PostToServiceAndReturnView(xyzLetterModel);
            }
    
            private ViewResult PostToServiceAndReturnView(BaseLetterModel model)
            {
                if (ModelState.IsValid)
                {
                    // do conversion here to service input
                    ServiceInput serviceInput = ToServiceInput(model);
                    _SomeService.Create(serviceInput);
    
                    return View("Success");
                }
                else
                {
                    return View("Create", model);
                }
            }
        }
    

    The View code could look like:

    @model BaseLetterModel
    @if (Model is ABCLetterModel)
    {
        using (Html.BeginForm("CreateABC", "Some"))
        {
            @Html.EditorForModel("ABCLetter")
        }
    }
    else if (Model is XYZLetterModel)
    {
        using (Html.BeginForm("CreateXYZ", "Some"))
        {
            @Html.EditorForModel("XYZLetter")
        }
    }
    

    You would still have an editor template for each model type.

    Another option is to have a custom model binder that figures out the type, based on some value in a hidden field, and then serializes it using that type.

    The first approach is much more preferred because the default model binder works well out of the box, and it’s a lot of maintenance to build custom model binders.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently in my CI project I have a single controller that handles all things
I have a Java project that I'm trying to implement with a model-view-controller design.
I have a controller action in my project that has a situation where it
I have created a new method in one of the project's controllers that it
I have a UIScrollView in my project. I have a view controller I would
I have a pretty straight forward uploadified page that allows users to upload files,
I have a small cakePHP app that allows for user's to login and be
I have an existing site that has a bunch of different models and controllers.
I have a fairly simple Rails application that allows users to manage their clients
I'm trying to develop a small Stripes project that allows the user to uoload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.