Copied to clipboard

Flag this post as spam?

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


  • lj 81 posts 425 karma points
    Nov 20, 2013 @ 12:25
    lj
    0

    dropdownlist loosing value

    Hi,

    Im using codefirst to create a form. the form has a dropdownlist field. when the form is submitted i am running some custom logic in the Validate() class. If the validation fails and the user is presented with the form again, the dropdownlist gets reset hence loosing the value the user selected.

    Am I missing something or is this a bug?

    thanks

  • lj 81 posts 425 karma points
    Nov 20, 2013 @ 14:20
    lj
    101

    Found workaround.

    In razor script umbracoContour/Views/FieldType.DropDownList.cshtml added code to read raw request.form values and tested if value existed for the submitted forms dropdownlist (you need to get the DropDownList control name it is a guid I found it using fiddler might be easier way) then mark corresponding value in DropDownList as selected="selected".

    see code below:

     

    @model Umbraco.Forms.Mvc.Models.FieldViewModel

    @{
        //customized to retain selected value on post back
       
        var selectedValue = "";
        if(Request.Form["controlguidhere"] != null)
        {
            selectedValue = Request.Form["controlguidhere"];
            selectedValue = selectedValue.Replace("\r\n", "");
        }
    }
    <select name="@Model.Name" id="@Model.Id"
    @if (Model.Mandatory){<text> data-val="true" data-val-required="@Model.RequiredErrorMessage"</text>}
    >
        <option value="">--- Please Select ---</option>
        @foreach (var pv in Model.PreValues)
        {
            var stripedValue = pv.Value.ToString().Replace("\r", "");
            <option value="@pv.Value"
            @if (Model.ContainsValue(pv.Value) || (selectedValue != "" && selectedValue == stripedValue))
            {<text>selected="selected"</text>}
            >@pv.Value</option>
        }
    </select>

     

  • Mike Parkinson 7 posts 57 karma points
    Feb 27, 2014 @ 09:48
    Mike Parkinson
    0

    So glad I found this solution.

    I have made some modifications as it did not quite work as expected and you don't need to find the control guid either:

    @model Umbraco.Forms.Mvc.Models.FieldViewModel
    @{
        // customised to retain selected value on post back
        var selectedValue = "";
        if (Request.Form[Model.Id] != null)
        {
            selectedValue = Request.Form[Model.Id];
        }
    }
    <select name="@Model.Name" id="@Model.Id"
    @if (Model.Mandatory){<text> data-val="true" data-val-required="@Model.RequiredErrorMessage"</text>}
    >
        <option value="">---Please Select---</option>
        @foreach (var pv in Model.PreValues)
        {
            <option value="@pv.Id.ToString()" 
            @if (Model.ContainsValue(pv.Value) || (selectedValue != "" && selectedValue == pv.Id.ToString()))
            {<text>selected="selected"</text>}
            >@pv.Value</option>
        }
    </select>

     

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft