Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom Engan 430 posts 1173 karma points
    May 24, 2017 @ 08:21
    Tom Engan
    0

    How to update dropdownlist in frontend stored in membership?

    I've succeed to load a dropdown gender list from member, and stored it with membership with use og this preValueHelper from this site: http://www.codeshare.co.uk/blog/how-to-use-an-umbraco-data-type-to-populate-an-mvc-drop-down-list/

    I now try to fill the gender dropdownlist like I do with preValueHelper before creating values in membership (and it works), but now I try to fill the same gender dropdownlist, but selected with the stored model.SelectedGender, but only get a "Error loading Partial View script" (no error when loading dropdown with preValueHelper).

    Umbraco version 7.6.1 .

    MODEL:
    public IEnumerable<SelectListItem> ListOfGenders { get; set; }
    public string SelectedGender { get; set; }
    
    
    VIEW:
    @Html.DropDownListFor(x => x.SelectedGender, Model.ListOfGenders, "-- Select --", new { @class = "form-control" })
    
    
    SURFACECONTROLLER:
    private PreValueHelper preValueHelper = new PreValueHelper();
    
    [ChildActionOnly]
    public ActionResult ReadHiker(HikerViewModel model)
    {
        if (this.Members.IsLoggedIn() && this.Members.IsMemberAuthorized(allowTypes: new[] { "hiker" }))
        {
            IMember memberService = Services.MemberService.GetByEmail(this.Members.CurrentUserName);
    
            foreach (SelectListItem gender in preValueHelper.GetPreValuesFromDataTypeId(1176))
            {
                preValueHelper.GetPreValuesFromDataTypeId(1176).Add(new SelectListItem()
                {
                    Text = gender.Text,
                    Value = gender.Value,
                    Selected = gender.Value == memberService.GetValue("gender").ToString() ? true : false
                });
            }
            //model.ListOfGenders = ____?? fill in the foreach loop above ??____
    
            //This line works fine for prevalues from membership without selected value
            //model.ListOfGenders = preValueHelper.GetPreValuesFromDataTypeId(1176);
    
            //These two fields also in the model, and it's rendering in view as it should.
            model.Mobile = memberService.GetValue("mobile").ToString();
            model.Biography = memberService.GetValue("biography").ToString();
        }
        return PartialView("UpdateHiker", model);
    }
    

    I also suspect that the view may be incorrect (I've modified the SelectListItem above several times.

    Model must be defined i surfacecontroller, and loaded into view, so how do I bind model.ListOfGender in SurfaceController into view this time?

    @Html.DropDownListFor(x => x.SelectedGender, Model.ListOfGenders, "-- Select --", new { @class = "form-control" })
    

    Any ideas?

  • Tom Engan 430 posts 1173 karma points
    May 24, 2017 @ 16:37
    Tom Engan
    100

    The solution:

    List<SelectListItem> selectedGenderList = new List<SelectListItem>();
    try
    {
        foreach (SelectListItem gender in preValueHelper.GetPreValuesFromDataTypeId(1176))
        {
            selectedGenderList.Add(new SelectListItem
            {
                Value = gender.Value,
                Text = gender.Text,
                Selected = gender.Value == memberService.GetValue("gender").ToString() ? true : false
            });
        }
        model.ListOfGenders = selectedGenderList;
    }
    catch (Exception ex)
    {
        // Not required, System.NullReferenceException if not selected
        model.ListOfGenders = preValueHelper.GetPreValuesFromDataTypeId(1176);
    }
    

    And now I don't need the "-- Select --" in view anymore..

    @Html.DropDownListFor(x => x.SelectedGender, Model.ListOfGenders)
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies