MVC - Set selected value of SelectList

后端 未结 14 1186
后悔当初
后悔当初 2020-12-13 08:03

How can I set the selectedvalue property of a SelectList after it was instantiated without a selectedvalue;

SelectList selectList = new SelectList(items, \"I         


        
14条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 08:52

    Doug answered my question... But I'll explain what my problem was exactly, and how Doug helped me solve my problem which you could be encountering.

    I call jquery $.post and am replacing my div with my partial view, like so.

    function AddNewAddress (paramvalue) {
        $.post(url, { param: paramvalue}, function(d) {
            $('#myDiv').replaceWith(d);
        });
    }
    

    When doing so, for some reason when stepping into my model my selected value affiliated property was never set, only until I stepped into the view it came into scope.

    So, What I had before

    @Html.DropDownListUnobtrusiveFor(model => model.CustomerAddresses[i].YearsAtAddress, Model.CustomerAddresses[i].YearsAtAddressSelectList, new {onchange = "return Address.AddNewAddress(this,'" + @Url.Action("AddNewAddress", "Address") + "'," + i + ")"})
    

    however even though Model.CustomerAddresses[i].YearsAtAddressSelectList, was set... it didn't set the selected value.

    So after....

     @Html.DropDownListUnobtrusiveFor(model => model.CustomerAddresses[i].YearsAtAddress, new SelectList(Model.CustomerAddresses[i].YearsAtAddressSelectList, "Value", "Text", Model.CustomerAddresses[i].YearsAtAddress), new { onchange = "return Address.AddNewAddress(this,'" + @Url.Action("AddNewAddress", "Address") + "'," + i + ")" })
    

    and it worked!

    I decided not to use DropDownListFor as it has problem when using unobtrusive validation, which is why i reference the following if your curious in a class classed

    HtmlExtensions.cs
    
    
    
    
    [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
    
    public static MvcHtmlString DropDownListUnobtrusiveFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList)
    {
        return DropDownListUnobtrusiveFor(htmlHelper, expression, selectList, null /* optionLabel */, null /* htmlAttributes */);
    
    }
    
    
    [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
    
    public static MvcHtmlString DropDownListUnobtrusiveFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, object htmlAttributes)
    {
        return DropDownListUnobtrusiveFor(htmlHelper, expression, selectList, null /* optionLabel */, new RouteValueDictionary(htmlAttributes));
    
    }
    
    
    [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
    
    public static MvcHtmlString DropDownListUnobtrusiveFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, IDictionary htmlAttributes)
    {
        return DropDownListUnobtrusiveFor(htmlHelper, expression, selectList, null /* optionLabel */, htmlAttributes);
    
    }
    
    
    [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
    
    public static MvcHtmlString DropDownListUnobtrusiveFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, string optionLabel)
    {
        return DropDownListUnobtrusiveFor(htmlHelper, expression, selectList, optionLabel, null /* htmlAttributes */);
    
    }
    
    
    [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
    
    public static MvcHtmlString DropDownListUnobtrusiveFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, string optionLabel, object htmlAttributes)
    {
        return DropDownListUnobtrusiveFor(htmlHelper, expression, selectList, optionLabel, new RouteValueDictionary(htmlAttributes));
    
    }
    
    
    [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Users cannot use anonymous methods with the LambdaExpression type")]
    
    [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
    
    public static MvcHtmlString DropDownListUnobtrusiveFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, string optionLabel, IDictionary htmlAttributes)
    {
        if (expression == null)
        {
            throw new ArgumentNullException("expression");
        }
    
    
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    
    
    
        IDictionary validationAttributes = htmlHelper
            .GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(expression), metadata);
    
    
    
        if (htmlAttributes == null)
            htmlAttributes = validationAttributes;
        else
            htmlAttributes = htmlAttributes.Concat(validationAttributes).ToDictionary(k => k.Key, v => v.Value);
    
    
    
        return SelectExtensions.DropDownListFor(htmlHelper, expression, selectList, optionLabel, htmlAttributes);
    
    }
    

提交回复
热议问题