Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc MVC自动完成验证_Asp.net Mvc_Jquery - Fatal编程技术网

Asp.net mvc MVC自动完成验证

Asp.net mvc MVC自动完成验证,asp.net-mvc,jquery,Asp.net Mvc,Jquery,在我的MVC应用程序中,我有一个使用JQuery/AJAX自动完成的视图,这是其中的一个摘录 <input type="search" name="searchPrimaryTrade" id="searchPrimaryTrade" data-scd-autocomplete="@Url.Action("AutocompletePrimaryTrade", "DataService")" style = "width: 300px;" data-val="

在我的MVC应用程序中,我有一个使用JQuery/AJAX自动完成的视图,这是其中的一个摘录

<input type="search" name="searchPrimaryTrade" id="searchPrimaryTrade" 
       data-scd-autocomplete="@Url.Action("AutocompletePrimaryTrade", "DataService")" 
       style = "width: 300px;" data-val="true" class="primaryTrade required" data-val-required="Must enter a Primary Trade"     />
<input type="button" id="ResetPrimaryTrade" value="Reset" class="resetSearch" data-scd-target-searchId="searchPrimaryTrade" style="margin-top:-6px; height:30px;"/><br/>
@Html.HiddenFor(model => model.PrimaryTradeId)
@Html.ValidationMessageFor(model => model.PrimaryTradeId, "*")


@Html.HiddenFor(model=>model.PrimaryTradeId) @Html.ValidationMessageFor(model=>model.PrimaryTradeId,“*”)
当用户选择要完成的项目时,我使用JQuery填充隐藏字段
@Html.HiddenFor(model=>model.PrimaryTradeId)

我的老板告诉我,如果用户在这个字段中输入了一个值,但没有从autocomplete中选择,那么当他从该字段中单击时,就会立即显示一条错误消息


如何实现这一点?

您可以创建如下自定义验证属性:

public class PrimaryTradeAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        if (value != null && value is int)
        {
            var unitOfWork = new UnitOfWork();
            int primaryTradeId = (int)value;

            // Code to check if the PrimaryTradeId exists in the database...
            if (unitOfWork.PrimaryTradeRepository().Find(primaryTradeId) == null)
            {
                return false;
            }
        }

        return true;
    }
}
[PrimaryTrade]
public int PrimaryTradeId { get; set; }
然后,您可以在模型中使用此属性,如下所示:

public class PrimaryTradeAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        if (value != null && value is int)
        {
            var unitOfWork = new UnitOfWork();
            int primaryTradeId = (int)value;

            // Code to check if the PrimaryTradeId exists in the database...
            if (unitOfWork.PrimaryTradeRepository().Find(primaryTradeId) == null)
            {
                return false;
            }
        }

        return true;
    }
}
[PrimaryTrade]
public int PrimaryTradeId { get; set; }

您可以创建如下自定义验证属性:

public class PrimaryTradeAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        if (value != null && value is int)
        {
            var unitOfWork = new UnitOfWork();
            int primaryTradeId = (int)value;

            // Code to check if the PrimaryTradeId exists in the database...
            if (unitOfWork.PrimaryTradeRepository().Find(primaryTradeId) == null)
            {
                return false;
            }
        }

        return true;
    }
}
[PrimaryTrade]
public int PrimaryTradeId { get; set; }
然后,您可以在模型中使用此属性,如下所示:

public class PrimaryTradeAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        if (value != null && value is int)
        {
            var unitOfWork = new UnitOfWork();
            int primaryTradeId = (int)value;

            // Code to check if the PrimaryTradeId exists in the database...
            if (unitOfWork.PrimaryTradeRepository().Find(primaryTradeId) == null)
            {
                return false;
            }
        }

        return true;
    }
}
[PrimaryTrade]
public int PrimaryTradeId { get; set; }