单选按钮列表不是从相同的代码生成的

时间:2010-12-16 23:33:53

标签: asp.net-mvc radio-button

我正在把头发拉出来。我有一个ASP.NET MVC网站,有两种形式,都有单选按钮。在第一页(有效),收音机列表显示正常。但是,在第二页上,单选按钮甚至不在源代码中。这是第一页的代码块(BestTimeType是我制作的枚举):

//Back End
[DisplayName("Time:")]
public RadioButtonListViewModel<BestTimeType> BestTimeRadioList { get; set; }

public EvalModel()
{
    BestTimeRadioList = new RadioButtonListViewModel<BestTimeType>
    {
        Id = "BestTime",
        SelectedValue = BestTimeType.Afternoon,
        ListItems = new List<RadioButtonListItem<BestTimeType>>
        {
            new RadioButtonListItem<BestTimeType>{Text = "Morning", Value = BestTimeType.Morning},
            new RadioButtonListItem<BestTimeType>{Text = "Afternoon", Value = BestTimeType.Afternoon},
            new RadioButtonListItem<BestTimeType>{Text = "Evening", Value = BestTimeType.Evening}
        }
    };
}
// Front End
<div class="grid_1 evalLabel reqField" style="padding-top: 5px;">
    <%= Html.LabelFor(model => model.BestTimeRadioList)%>
</div>
<div class="grid_4" style="text-align: center; padding: 5px 0px 10px 0px;">
    <%= Html.RadioButtonListFor(model => model.BestTimeRadioList) %>
</div>

以下是第二页的代码块:

//Back End
[Required(ErrorMessage = "*")]
[DisplayName("HS Diploma:")]
public RadioButtonListViewModel<bool> HsDiplomaRadioList { get; set; }

public EmploymentModel()
{
    HsDiplomaRadioList = new RadioButtonListViewModel<bool>
    {
        Id = "HsDiploma",
        SelectedValue = false,
        ListItems = new List<RadioButtonListItem<bool>>
        {
            new RadioButtonListItem<bool> {Text = "Yes", Value = true},
            new RadioButtonListItem<bool> {Text = "No", Value = false}
        }
    };

}

//Front End
<div class="grid_2 employLabel reqField">
    <%= Html.LabelFor(model => model.HsDiplomaRadioList) %>
</div>
<div class="grid_3">
    <%= Html.RadioButtonListFor(model => model.HsDiplomaRadioList)%>
    <%= Html.ValidationMessageFor(model => model.HsDiplomaRadioList)%>
</div>

我也尝试为HsDiplomaRadioList制作一个自定义的Enum(是/否),但收音机也没出现。

我必须遗漏一些非常愚蠢的简单。如果需要更多代码,我很乐意将它们放好。

提前致谢。

修改

以下是RadioButtonList的代码:

public static class HtmlHelperExtensions
{
    public static string RadioButtonListFor<TModel, TRadioButtonListValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel<TRadioButtonListValue>>> expression) where TModel : class
    {
        return htmlHelper.RadioButtonListFor(expression, null);
    }

    public static string RadioButtonListFor<TModel, TRadioButtonListValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel<TRadioButtonListValue>>> expression, object htmlAttributes) where TModel : class
    {
        return htmlHelper.RadioButtonListFor(expression, new RouteValueDictionary(htmlAttributes));
    }

    public static string RadioButtonListFor<TModel, TRadioButtonListValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel<TRadioButtonListValue>>> expression, IDictionary<string, object> htmlAttributes) where TModel : class
    {
        var inputName = GetInputName(expression);

        RadioButtonListViewModel<TRadioButtonListValue> radioButtonList = GetValue(htmlHelper, expression);

        if (radioButtonList == null)
            return String.Empty;

        if (radioButtonList.ListItems == null)
            return String.Empty;

        var divTag = new TagBuilder("div");
        divTag.MergeAttribute("id", inputName);
        divTag.MergeAttribute("class", "radio");
        foreach (var item in radioButtonList.ListItems)
        {
            var radioButtonTag = RadioButton(htmlHelper, inputName, new SelectListItem { Text = item.Text, Selected = item.Selected, Value = item.Value.ToString() }, htmlAttributes);

            divTag.InnerHtml += radioButtonTag;
        }

        return string.Concat(divTag, htmlHelper.ValidationMessage(inputName, "*"));
    }

    public static string GetInputName<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
    {
        if (expression.Body.NodeType == ExpressionType.Call)
        {
            var methodCallExpression = (MethodCallExpression)expression.Body;
            string name = GetInputName(methodCallExpression);
            return name.Substring(expression.Parameters[0].Name.Length + 1);

        }
        return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1);
    }

    private static string GetInputName(MethodCallExpression expression)
    {
        // p => p.Foo.Bar().Baz.ToString() => p.Foo OR throw...

        var methodCallExpression = expression.Object as MethodCallExpression;
        if (methodCallExpression != null)
        {
            return GetInputName(methodCallExpression);
        }
        return expression.Object.ToString();
    }

    public static string RadioButton(this HtmlHelper htmlHelper, string name, SelectListItem listItem,
                         IDictionary<string, object> htmlAttributes)
    {
        var inputIdSb = new StringBuilder();
        inputIdSb.Append(name)
            .Append("_")
            .Append(listItem.Value);

        var sb = new StringBuilder();

        var builder = new TagBuilder("input");
        if (listItem.Selected) builder.MergeAttribute("checked", "checked");
        builder.MergeAttribute("type", "radio");
        builder.MergeAttribute("value", listItem.Value);
        builder.MergeAttribute("id", inputIdSb.ToString());
        builder.MergeAttribute("name", name + ".SelectedValue");
        builder.MergeAttributes(htmlAttributes);
        sb.Append(builder.ToString(TagRenderMode.SelfClosing));
        sb.Append(RadioButtonLabel(inputIdSb.ToString(), listItem.Text, htmlAttributes));
        //sb.Append("<br>");

        return sb.ToString();
    }

    public static string RadioButtonLabel(string inputId, string displayText,
                                 IDictionary<string, object> htmlAttributes)
    {
        var labelBuilder = new TagBuilder("label");
        labelBuilder.MergeAttribute("for", inputId);
        labelBuilder.MergeAttributes(htmlAttributes);
        labelBuilder.InnerHtml = displayText;

        return labelBuilder.ToString(TagRenderMode.Normal);
    }


    public static TProperty GetValue<TModel, TProperty>(HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) where TModel : class
    {
        TModel model = htmlHelper.ViewData.Model;
        if (model == null)
        {
            return default(TProperty);
        }
        Func<TModel, TProperty> func = expression.Compile();
        return func(model);
    }
}

好吧终于开始堆栈跟踪了(我应该先做这个,但是我昨晚很匆忙)并且发现由于某种原因model.HsDiplomaRadio为空。我需要找出原因。

0 个答案:

没有答案