当模型是列表时验证搜索输入

时间:2015-05-17 11:16:20

标签: asp.net asp.net-mvc-5 unobtrusive-validation

在视图中,模型为:

@model List<ParamsTaskLib.ParamRecord>

我想创建一个带有输入字段的表单,该字段将是要搜索的DeviceID:

public class ParamRecord
{
    [Required]
    [RegularExpression(@"^[0-9a-fA-F]{8}$", ErrorMessage = "Invalid Device ID (hex)")]
    public Int64 DeviceID { get; set; }

    other fields in here...

我尝试按以下方式编辑视图:

<form method="get" >
        @Html.EditorFor(model => model.First().DeviceID)
        @Html.ValidationMessageFor(model => model.First().DeviceID)

但它显然不起作用,它似乎不应该工作:) 我很困惑如何使用列表作为模型,并结合搜索框......

我得到的错误是“Value not not NULL”在这一行:

            @Html.EditorFor(model => model.First().DeviceID)

额外说明:此页面的目的是显示一个表,其中包含数据库中特定DeviceID的许多行(ParamRecord)。 表单+输入字段的目的是为Web服务器提供我想要查看数据的DeviceID(ParamRecord的集合)

这是完整的查看页面:

@model List<ParamsTaskLib.ParamRecord>

@{
ViewBag.Title = "SearchResults";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<script type="text/javascript" >
    function UpdateFunc() {
        $.get("/home/GetProgress", function(data) {
            $("#UpdatePercentage").text(data);
            if (data == "100")
                location.reload();
            else
                setTimeout(UpdateFunc, 2000);                
        });
    }

    function Update() {
        var id = $("#id").val();
        $.post("@Url.Content("~/Home/UpdateID/")" + id, function (data) {
            UpdateFunc();
        });               
        $("#UpdateAni").attr("src", "@Url.Content("~/Content/Images/ajax-loader.gif")");
        $("#UpdatePercentage").show();
    }     

</script>

<div>
    <form method="get" >
        @Html.TextBox("id")
        @*@Html.EditorFor(model => model.First().DeviceID)*@
        @Html.ValidationMessageFor(model => model.First().DeviceID)
        <input type="submit" value="Search" />
        <img id="UpdateAni" src="@Url.Content("~/Content/Images/ajax-loader_static.gif")" onclick="Update()"/>
        <span id="UpdatePercentage" style="display:none" >0</span>
    </form>

    @if (Model != null )
    {
        <h4>ID 0x<span>@ViewBag.ID</span> , last update was at @ViewBag.LastUpdateTS</h4>
        <h3>Total @ViewBag.NumOfParams parameters</h3>
        <hr />
        <table border="1" >
            <thead>
                <tr>
                    <th>Parameter Number</th>
                    <th>Device Inner ID</th>
                    <th>Device Type</th>
                    <th>Value (Int)</th>
                    <th>Value (Float)</th>
                    <th>Value (String)</th>
                </tr>                    
            </thead>
            @for (int i = 0; i < Model.Count; i++)
            {   
                <tr>
                    <td>@Model[i].ParamIndex</td>
                    <td>@Model[i].DeviceInnerID</td>
                    <td>@Model[i].DeviceType</td>                        
                    <td>@Model[i].ParamValInt</td>                        
                    <td>@Model[i].ParamValFloat</td>
                    <td>@Model[i].ParamValSTR</td>                    
                </tr>
            }
        </table>
    }
    else if (ViewBag.NoResults == 1)
    {
        <h3>No results for ID 0x<span>@ViewBag.ID</span></h3>
    }

</div>

0 个答案:

没有答案