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 8288513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:17:50+00:00 2026-06-08T12:17:50+00:00

I’ve been struggling with this annoying problem. I have a Model, Editor and controller:

  • 0

I’ve been struggling with this annoying problem. I have a Model, Editor and controller:

public class TeamDetails{
   public int SeasonId{get;set;}

   public TeamDetails()
   {
      //need SeasonId to be populated already from form here, but it won't yet.
   }

   public TeamDetails(int seasonId)
        : this()
    {
      //load dropdownlist using seasonId from repository
    }
}

//Controller

//get method gets seasonId from querystring.
 public ViewResult Create(int seasonId)
 {
    // calls the parameterized constructor for TeamDetails
    // which uses seasonId to populate a dropdownlist from db repo.
     var teamDetails = new TeamDetails(seasonId);
 }

[HttpPost]
public PartialViewResult Create(TeamDetails model)
{
   // no such luxury here to call the parameterized 
   // Constructor for model. additional logic.
}

//editor

@using (Ajax.BeginForm("Create", "Team", options))
{
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.SeasonId)
    //additional fields
}

I need to have the SeasonId from the hidden field already be populated by the time TeamDetails constructor is hit as I need that in the constructor. However, that obviously can’t happen because the constructor is called before the MVC has a chance to assign SeasonId to the object. Any way I can get around this?

Thanks

Riz

  • 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-06-08T12:17:52+00:00Added an answer on June 8, 2026 at 12:17 pm

    I would advise against embedding logic like this in your view models (such as determining team data from a SeasonId). Treat your ViewModels as plain flat objects with properties only. Apply what ever input validation attributes you want to on your view models, and then in your action that handles the postback check your modelstate for validation problems. Business rules validation is probably something you won’t be able to do in the controller as you may need information from other sources like the database etc. Those should be in your business layer, I raise these problems as a DomainException, and then in my controller capture them in a try catch and add any DomainExceptions to the ModelState as a ModelError. Then do your normal test if ModelState.IsValid. Once your model is valid you can create an internal instance of your TeamDetails class if you must and deal with it’s inner working under your full control. Though I would recommend keeping the actual business logic of what you do with the view data in a business layer and not in the controller. Think of the controller as a means of passing information from the view models to your business/service layer and that’s it.

    Here’s some pseudo code to show what I mean, I haven’t checked if this is syntactically correct but you get the idea.

    public class TeamsCreateFormModel
    {
        public int TeamId { get; set; }
        [Required]
        public int SeasonId { get; set; }
        [Required]
        public string Name { get; set; }
        public int Color { get; set; }
    }
    

    .

    [HttpPost, ValidateAntiForgeryToken]
    public ActionResult Create(TeamsCreateFormModel model)
    {
        if (!ModelState.IsValid)
            return RedirectToAction("Create");
        try
        {
            Team team = new Team() {Name = model.Name, 
                                    Color = model.Color, 
                                    SeasonId = model.SeasonId}
    
            var teamService = new TeamService(); //DAL encapsulated in this class
            team = teamService.Create(team);
        }
        catch (DomainException ex)
        {
            ModelState.AddModelError(string.Empty, ex.Message);
            return RedirectToAction("Create");
        }
        return RedirectToAction("Details", new { id = team.TeamId});
    }
    

    EDIT:
    For dealing with dropdowns and other things that you need to have pre-populated I would suggest also using a view model. The easiest way is to just inherit from your model expected for the postback and add on any properties that you need in the view but don’t expect to be posted back to the server upon submit.

    public class TeamsCreateViewModel : TeamsCreateFormModel
    {
        public IEnumerable<SelectListItem> Seasons
    }
    

    For simplicity, I am using Linq to get a collection of SelectListItem from the collection GetSeasons() is likely to return. There are other considerations to look at as well such as having the first item be something like “— Please pick one—” or the like

    public ActionResult Create()
    {
        var teamService = new TeamService(); //DAL encapsulated in this class
        var model = new TeamsCreateViewModel();
        model.Seasons = teamService.GetSeasons().Select(x => new SelectListItem() 
                                       {Text = "TextHere", Value = "ValueHere"});
        return View("Create");
    }
    

    The view would have something like this…

    @model TeamsCreateViewModel
    ...
    @Html.DropDownListFor(x => x.SeasonId, Model.Seasons)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.