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

  • Home
  • SEARCH
  • 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 6170585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:03:47+00:00 2026-05-23T23:03:47+00:00

i have this controller: public ActionResult Novo() { var products = context.Product.Select(x => new

  • 0

i have this controller:

public ActionResult Novo()
{
   var products = context.Product.Select(x => new SelectListItem
   {
      Text = x.Name,
      Value = SqlFunctions.StringConvert((double)x.Id).Trim()
   }).ToList();

   MyViewModel myViewModel= new MyViewModel()
   {
      Products = products
   };

   return View(myViewModel);
}

[HttpPost]
public ActionResult Novo(MyViewModel myViewModel)
   {
      if (ModelState.IsValid)
      {
         ...

         context.SaveChanges();

         return RedirectToAction("Index");
      } else {
         var products = context.Product.Select(x => new SelectListItem
         {
            Text = x.Name,
            Value = SqlFunctions.StringConvert((double)x.Id).Trim()
         }).ToList();

         MyViewModel myViewModel= new MyViewModel()
         {
            Products = products
         };

         return View(myViewModel);
      }
}

I must to populate Products in ViewModel in this two methods.

My ViewModel:

public class MyViewModel
{
   public IEnumerable<SelectListItem> Products { get; set; }
   public string ProductIdSelected { get; set; }
}

My HTML:

<div class="editor-field">
   @Html.DropDownListFor(model => model.ProductIdSelected, Model.Products)
</div>

The problem is in my Controller, i must to populate ViewModel in this two methods. I don’t like that, I don’t like to create a method to populate ViewModel too because when validation fail, the others fields keeps populated, why my Products fields don’t keeps populated too? I think using a method is ugly. Is there a way to avoid to use another method?

Is there a way to populate Producs in my ViewModel only once and cache the list of Products when someone do a Post and the Post is not valid.

If NOT, the better way is to use another method. Thanks.

  • 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-23T23:03:48+00:00Added an answer on May 23, 2026 at 11:03 pm

    What’s wrong with creating a separate method to populate your VM (for the GET and for the POST when validation fails)?

    [HttpGet]
    public ActionResult Novo()
    {
        MyViewModel myViewModel= new MyViewModel();
        this.LoadProducts(myViewModel);
        return View(myViewModel);
    }
    
    [HttpPost]
    public ActionResult Novo(MyViewModel myViewModel)
    {
        if (ModelState.IsValid)
        {
             ...
    
             context.SaveChanges();
    
             return RedirectToAction("Index");
        }
        else 
        {
             MyViewModel myViewModel= new MyViewModel();
             this.LoadProducts(myViewModel);
             return View(myViewModel);
        }
    }
    
    private void LoadProducts(MyViewModel model)
    {
        model.Products = context.Product.Select(x => new SelectListItem
        {
            Text = x.Name,
            Value = SqlFunctions.StringConvert((double)x.Id).Trim()
        }).ToList();
    }
    

    Additionally, you could provide another parameter for LoadProducts so that it could set the selected item.

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

Sidebar

Related Questions

I have this controller: public ActionResult Details(String UserName) { using (var db = new
I have this in my controller public ActionResult Testing() { CustomerContactModel model = new
New to mvc, I have a controller like this public ActionResult Index(DateTime Date, DateTime
I have this controller: public ActionResult Index(int id) { var cust = (from c
I have this controller [HttpPost] public ActionResult Compose(ComposeMessage composeMessage) { var message = ;
I have got this controller: public class NewPostController : Controller { List<SelectListItem> languages= new
I have this in my Controller. public function delete($id) { if($this->request->is('get')) { throw new
I have this code in my controller: [HttpGet] public ActionResult Register(UserRegistrationModel model) { return
i have this controller function: Public Function Index(ByVal id As System.Nullable(Of Integer)) As ActionResult
I have this code in the controller public ActionResult Close(string cName) { return RedirectToAction(action1,

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.