在视图中,模型为:

@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)

但这显然不起作用,似乎不应该起作用:)我对如何将列表作为模型,与搜索框结合使用感到困惑...

我得到的错误是此行上的“值不能为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>

  ask by Alonzzo2 translate from so

本文未有回复,本站智能推荐: