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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:16:19+00:00 2026-05-27T07:16:19+00:00

I searches for hours (or days) and didn’t find a solution yet. I want

  • 0

I searches for hours (or days) and didn’t find a solution yet. I want to edit a customer with a DropdownListFor for the salutation with the right preselected value.

I’ve got 3 entities (Database first concept, this is not my own design…): customer, address, salutation

A CUSTOMER has an address_id (f_key) and an ADDRESS has got a salutation_id (f_key). The ADDRESS holds the first and last name for example. Inside the SALUTATION entity there is a column sal1 which holds all possible salutations.

Now, I want to edit my customer via a ViewModel which looks like this:

public class CustomerViewModel
{
    public CUSTOMER cust { get; set; }
    public SelectList salutationList { get; set; }

    CustomerRepository repository = new CustomerRepository();

    public CustomerViewModel(int id)
    {
        cust = repository.GetCustomerByIdAsQueryable(id).Single();
        salutationList = new SelectList(repository.GetSalutations(), cust.ADDRESS.SALUTATION.SAL1);
    }
    // Some more
}

The CutsomerRepository methods:

public class CustomerRepository
    {
        private MyEntities db = new MyEntities();

        public IQueryable<CUSTOMER> GetCustomerByIdAsQueryable(int id) {...}

        public IQueryable<CUSTOMER> GetCustomersByName(string firstName, string lastName, int maxCount)  {...}

        public List<string> GetSalutations()
        {
            var salutationList = new List<string>();
            var salutationListQry = from s in db.SALUTATION
                                    select s.SAL1;
            salutationListTemp.AddRange(salutationListQry);
            return salutationList;
        }
        // ...
    }

This is my controller method:

[HttpPost]
public ActionResult CustomerData(int id, FormCollection fc)
{
    var vm = new CustomerViewModel(id);
    // Why do I need the following line?
    vm.cust = repository.GetCustomerByIdAsQueryable(id).Single();
    try
    {
        UpdateModel(vm, fc);
        repository.Save();
        return View("CustomerData", vm);
    }
    catch (Exception e)
    {
        return View();
    }
}

And finally the part from my View:

@model WebCRM.ViewModels.CustomerViewModel
@using (Html.BeginForm())
{
    // ...
    <div class="editor-label">
        @Html.Label("Salutation:")
        @Html.DropDownListFor(model => model.cust.ADDRESS.SALUTATION.SAL1, Model.salutationList)
        // @Html.DropDownList("Salutation", Model.salutationList)
    </div>
    <div class="editor-label">
    </div>
    <div class="editor-field">
        @Html.Label("Last name:")
        @Html.EditorFor(model => model.cust.ADDRESS.LASTNAME)
        @Html.ValidationMessageFor(model => model.cust.ADDRESS.LASTNAME)
    </div>
    <p>
        <input type="submit" value="Speichern" />
    </p>
}

Changing and saving last names works fine. But when saving the salutation it changes the SAL1 value in the SALUTATION entity to the one I’ve chosen in the DropdownListFor. What I want is to change the salutation_id inside the ADDRESS entity for my customer. Why isn’t that working?

Another strange behavoior: When removing the marked line in my CustomerController, I can’t even change and save the last name. Normally the constructor of the CustimerViewModel sets the customer. So why do I have to have the line of code for setting the customer inside my ViewModel? It’s duplicated, but has to be there…

Thanks in advance.

  • 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-27T07:16:19+00:00Added an answer on May 27, 2026 at 7:16 am

    You need to have Selected property in your list.

    I can show you my working example:

    public static IEnumerable<SelectListItem> GetCountries(short? selectedValue)
        {
            List<SelectListItem> _countries = new List<SelectListItem>();
            _countries.Add(new SelectListItem() { Text = "Select country...", Value = "0", Selected = selectedValue == 0 });
            foreach (var country in ObjectFactory.GetInstance<DataRepository>().GetCountries())
            {
                _countries.Add(new SelectListItem()
                {
                    Text = country.Name,
                    Value = country.ID.ToString(),
                    Selected = selectedValue > 0 && selectedValue.Equals(country.ID)
                });
            }
            return _countries;
        }  
    

    In controller i store this into viewbag:

    ViewBag.Countries = CompanyModel.GetCountries(0);
    

    In view:

    @Html.DropDownListFor(m => m.CompanyModel.CountryId, (IEnumerable<SelectListItem>)ViewBag.Countries)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I spent several hours reading through docs and forums, trying to find a solution
Spent several hours trying to find a solution to the 'simple' problem of converting
I'm struggling to find the solution of this for hours, but nothing seems to
i searched for 2 days about my problem find a lot of solution but
i searched for hours for a way to solve my problem but didn't get
I have searched for hours now and haven't found a solution for my problem.
I have searched for a good couple hours now, looking for a solution to
I've already spent countless hours puzzling over this, utilizing Google searches and other Stack
First time posting on stack overflow. I've spent hours scouring over many Google searches
I want to find the top 1000 documents in a Lucene.NET index that match

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.