1

An article view model

 public class ArticleViewModel : ViewModelBase
    {

        [Required(ErrorMessage = "Required")]
        public string Title { get; set; }
        [Required(ErrorMessage = "Choose the language")]
        public BELocale Locale { get; set; }
}

public class BELocale : BEEntityBase
    {

        public string OriginalName { get; set; }
        public string FriendlyName { get; set; }
        public string TwoLetterISOName { get; set; }
    }

A view "AddLocaleForArticle"

@model Models.ArticleViewModel

@using (Html.BeginForm("VefifyAddingLocaleForArticle", "Administration"))
{

 @Html.TextBoxFor(m => m.Title, new { disabled = "disabled" })
 @Html.DropDownListFor(m => m.Locale,
                    new SelectList(ViewBag.AvalaibleLocales, "ID", "OriginalName"),   "Select a language"
               )
   @Html.ValidationMessageFor(m => m.Locale)
   <input type="submit" value="Save" />         
}

An action

public ActionResult VefifyAddingLocaleForPhoto(ArticleViewModel article)
        {
            //article.Locale == null for some reason.
             //but article.Title isn't null, it contains the data
            return RedirectToAction("AddingLocaleForPhotoSuccess", "adminka");
        }

Why article.Locale is equal null and how to fix it?

4
  • Check the name of your dropdownlist in HTML Commented Oct 30, 2011 at 8:44
  • The name is "Locale" in HTML. Commented Oct 30, 2011 at 8:45
  • "ID" doesn't seem to be present in your viewmodel Commented Oct 30, 2011 at 8:53
  • ViewModelBase and BEEntityBase contain ID. Commented Oct 30, 2011 at 8:55

2 Answers 2

2

When the form is submitted a dropdown list sends only the selected value to the controller. So you cannot expect it to populate an entire complex object such as BELocale using a dropdownlist. The best you could is to populate its ID property and fetch the remaining of the object from your data store using this id.

So you will have to modify your dropdownlist helper so that it is bound to the id property of the locale as first argument:

@Html.DropDownListFor(
    m => m.Locale.ID,
    new SelectList(ViewBag.AvalaibleLocales, "ID", "OriginalName"),   
    "Select a language"
)

Now inside the corresponding controller action you will get the id:

public ActionResult VefifyAddingLocaleForPhoto(ArticleViewModel article)
{
    // article.Locale.ID will contain the selected locale id
    // so you can use this information to fetch the corresponding BELocale object
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Html.DropDownList("ddlCountry", new SelectList(ViewBag.lstCountry, "CountryID", "CountryName")). On selecting a particular country, I want to pass the CountryID to the countroller. How can it be done using jQuery? NOTE: I'm not using Strongly typed View
0

You may fill dropdown like this in your view model

public List<KeyValuePair<int, String>> locale
        {
            get
            {
               return  _localerepo.Findlocals().Select(x => new KeyValuePair<int, string>(x.ID, x.OriginalName)).ToList();
            }
        }

In your view use this

<%:Html.DropDownListFor(x => x.ID, new SelectList(Model.locale, "key", "value"), "--Select locale--")%>
                <%= Html.ValidationMessageFor(model => model.ID)%>

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.