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.
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
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 backvarselectedValue="";if(Request.Form[Model.Id]!=null){selectedValue=Request.Form[Model.Id];}}<selectname="@Model.Name"id="@Model.Id"@if(Model.Mandatory){<text>data-val="true"data-val-required="@Model.RequiredErrorMessage"</text>}><optionvalue="">---Please Select---</option>@foreach(varpvinModel.PreValues){<optionvalue="@pv.Id.ToString()"@if(Model.ContainsValue(pv.Value)||(selectedValue!=""&&selectedValue==pv.Id.ToString())){<text>selected="selected"</text>}>@pv.Value</option>}</select>
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
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>
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:
Thanks
is working on a reply...