1

我有以下型号:

public class EvaluationCriteriaItem
{
    public int ID { get; set; }
    public string Name { get; set; }
    [Required(ErrorMessage = "*")]
    public int Score { get; set; }
    public List<EvaluationCriteriaItem> Children { get; set; }
}

public class UserPanelModel
{
    public List<SelectListItem> OrganizationsList { get; set; }
    public List<EvaluationCriteriaItem> EvaluationCriteriaList { get; set; }
}

我用 Razor 从上面的模型创建了一个 TreeView :

@using AssessmentSystem.Models
@section Header{
    <link href="@Url.Content("~/Content/jquery.treeview.rtl.css")" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.treeview.user.js")"></script>
    <style type="text/css">
        input[type="text"], input[type="password"]
        {
            width: 40px;
            margin-right: 10px;
            direction: ltr;
        }
        .treeview .hover, .filetree div.file:hover
        {
            cursor: default;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function () {
            $('input[type="text"]').keydown(function (event) {
                if (event.keyCode == 46 || event.keyCode == 8 ||
                    event.keyCode == 9 || event.keyCode == 27 ||
                        (event.keyCode == 65 && event.ctrlKey === true) ||
                            (event.keyCode >= 35 && event.keyCode <= 39))
                    return;
                else if ((event.keyCode < 48 || event.keyCode > 57) &&
                    (event.keyCode < 96 || event.keyCode > 105))
                    event.preventDefault();
            });
        });
    </script>
}
@helper TreeView(List<EvaluationCriteriaItem> childrens)
    {
        for (int i=0; i<childrens.Count; i++)
        {
            if (childrens[i].Children != null && childrens[i].Children.Count > 0)
            {
    <li>
        <div class="folder">
            <div>
                <span id="@string.Format("span_{0}", childrens[i].ID)">@childrens[i].Name</span>
            </div>
        </div>
        <ul>
            @TreeView(childrens[i].Children)
        </ul>
    </li>
            }
            else
            {
    <li>
        <div class="file">
            <div>
                <span id="@string.Format("span_{0}", childrens[i].ID)">@childrens[i].Name</span>
                @Html.TextBox("ScoreFor_", childrens[i].Score == 0 ? "" : childrens[i].Score.ToString(), new { maxlength = 3 })
            </div>
        </div>
    </li>
            }
        }
}
@if (!MvcHtmlString.IsNullOrEmpty(Html.ValidationMessage("InvalidData")))
{
    <div style="width: 250px; margin: 20px auto; color: red;">
        @Html.ValidationMessage("InvalidData")
    </div>
}
@using (Html.BeginForm("SubmitScore", "UserPanel"))
{
    <table class="register-table">
        <tr>
            <td>
                Organization :
            </td>
            <td>
                @Html.DropDownListFor(x => x.OrganizationsList, Model.OrganizationsList)
            </td>
        </tr>   
    </table>
    <div style="width: 50%; margin: 0 auto;">
        <ul id="EvaluationCriteriaTreeView" class="filetree">
            @TreeView(Model.EvaluationCriteriaList)
        </ul>
        <br />
        <div style="text-align: center;">
            <input type="submit" value="Submit Score" /></div>
    </div>
}
<script type="text/javascript">
    $("#EvaluationCriteriaTreeView").treeview({
        animated: "fast"
    });
</script>

但是因为我正在使用@Html.TextBox我不能使用服务器模型验证。
我如何@Html.TextBoxFor在最佳情况下使用。

谢谢

4

1 回答 1

1

我建议在这种情况下使用 MVC Display/EditorTemplates 而不是助手。使用模板,您可以获得自动收集支持,并且您可以使用强类型模型。

https://web.archive.org/web/20151112171922/http://blogs.msdn.com/b/nunos/archive/2010/02/08/quick-tips-about-asp-net-mvc-ui-助手和模板.aspx

https://web.archive.org/web/20150908050041/http://blogs.msdn.com/b/nunos/archive/2010/02/08/quick-tips-about-asp-net-mvc-editor-模板.aspx

于 2012-04-07T16:37:02.847 回答