4

我正在做一个项目,我的验证有一些问题这是我的模型:

[ColumnMap("Guest_ID")]
public int Guest_ID { get; set; }

[ColumnMap("FirstName")]
[Required]
[Display(Name="FirstName")]
public  string FirstName { get; set; }

[ColumnMap("LastName")]
public string LastName { get; set; }

[ColumnMap("Phone")]
[Required]  
[MaxLength(10)]
public Nullable<int> Phone { get; set; }

[ColumnMap("AdrOras")]
[Required]
public string AdrOras { get; set; }

所以我要求 onFirstName并且PhoneMaxLength=10 on Phone

这是我CreateReservation在控制器中的方法:

public ActionResult CreateReservation(int? RoomID, string FirstName, string LastName,int? Phone,string Oras,string Judet,string Tara,string Strada,int? GuestTypeId,DateTime? Birthday,DateTime? Data_Check_in, DateTime? Data_Check_out) 
{
    DBContext.Current.Open();
    Reservation rezervare = new Reservation();
    string  username= User.Identity.Name;
    Users user=Users.UserbyUsername(username);
    if(!string.IsNullOrEmpty(RoomID.ToString())&&!string.IsNullOrEmpty(FirstName)&&!string.IsNullOrEmpty(LastName)&&Phone.HasValue&&!string.IsNullOrEmpty(Judet)&&!string.IsNullOrEmpty(Tara)&&!string.IsNullOrEmpty(Strada)&&!string.IsNullOrEmpty(GuestTypeId.ToString())&&Birthday.HasValue&&Data_Check_in.HasValue&& Data_Check_out.HasValue)
    {
        rezervare.CreazaRezervare(RoomID, FirstName, LastName, Phone, Oras, Judet, Tara, Strada, GuestTypeId, Birthday,user.UserID,Data_Check_in, Data_Check_out);
        return RedirectToAction("MyReservation","Reservation");
    }

    var model = GuestTyp.SelectAll();
    ReservationView rez = new ReservationView()

    {
        rezervare =rezervare,
        client = model.ToList()
    };

    DBContext.Current.Close();
    return View(rez);
}

这是我的观点:

@model LicentaTest.Models.ReservationView
@using JQueryUIHelpers

@{
    ViewBag.Title = "Checkout";
}

<head>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="../../Scripts/jquery-ui-1.8.19.min.js" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui.unobtrusive.min.js")" type="text/javascript"></script>   
    <script src="../../Scripts/jquery-ui.unobtrusive-0.5.0.min.js" type="text/javascript"></script>
</head>

<script type="text/javascript">
    $(function () {
        $('input[type=text], textarea, input[type=password]').width(200);
        $('#valSum').hide();
        $('form').bind('invalid-form.validate', function (error, element) {
            $('#valSum').show("slow");
            return false;
        });
    });
</script>

@if (!ViewData.ModelState.IsValid) {
    <script type="text/javascript">
        $(function () {
            $('#valSum').show("slow");
        });
    </script>
}

@using (Html.BeginForm("CreateReservation", "Reservation", FormMethod.Post))
{
    @Html.ValidationSummary(true)
    <form class="form">
    <div class = "styler">
    <div id = "valSum" class="ui-state-error ui-corner-all" style="padding: 0 .7em;width:500px">    
</div>
    <h6>Completare Date Rezervare</h6>
    <fieldset class="ui-widget">
        <legend class="ui-state-legend-default ui-corner-top ui-corner-bottom">Date personale</legend>
            @Html.Hidden("RoomID")  
            <div class="editor-label">
                @Html.Label("FirstName")
                @Html.TextBox("FirstName") 
                @Html.ValidationMessage("FirstName","Numele trebuie introdus")
            </div>
            <div class="editor-label">
                @Html.Label("LastName    ")
                @Html.TextBox("LastName") 
            </div>
            <div class="editor-label">
                @Html.Label("Phone")
                @Html.TextBox("Phone")  
                @Html.ValidationMessage("Phone","Numarul trebuie sa fie din maxim 10 cifre")
            </div>
            <div class="editor-label">
                @Html.Label("Data Nasterii")
                @(Html.JQueryUI().Datepicker("Birthday").MaxDate(DateTime.Now))
            </div>
            <div class="editor-label">
                @Html.Label("Tip CLient")
                @Html.DropDownList("GuestTypeID", new SelectList(Model.client.AsEnumerable(), "GuestTypeID", "GuestType"))
            </div>
        </fieldset>
    <fieldset class="ui-widget">
    <legend class="ui-state-legend-default ui-corner-top ui-corner-bottom">Adresa</legend>
        <div class="editor-label">
            @Html.Label("Tara")
            @Html.TextBox("Tara")
        </div>
        <div class="editor-label">
            @Html.Label("Oras")
            @Html.TextBox("Oras")
            @Html.ValidationMessage("Oras","Field is required")
        </div>
        <div class="editor-label">
            @Html.Label("Judet")
            @Html.TextBox("Judet")
        </div>
        <div class="editor-label">
            @Html.Label("Strada")
            @Html.TextBox("Strada")
        </div>
    </fieldset>
    <fieldset class="ui-widget">
        <legend class="ui-state-legend-default ui-corner-top ui-corner-bottom">Perioada rezervare</legend>
        <div class="editor-label">
            @Html.Label("Data Intrare")
            @Html.JQueryUI().Datepicker("Data_Check_in")
        </div>
        <div class="editor-label">
            @Html.Label("Data Iesire")
            @Html.JQueryUI().Datepicker("Data_Check_out")
        </div>
    </fieldset>

    <p>
        <input type="submit" value="Create" />
    </p>

    <div>
        @Html.ActionLink("Back to List", "Index", "Guest")
    </div>
    </div>
    </form>
}

当我按下创建按钮时,maxlenght 验证正在工作,但所需的字段验证不起作用 在此处输入图像描述

为什么会发生这种情况我做错了什么

4

2 回答 2

4

您有 2 个相互嵌套的表单。您不能有嵌套表单。

你有:

@using (Html.BeginForm("CreateReservation", "Reservation", FormMethod.Get)){
    <form>...</form>
}

删除<form></form>标签

head另外...我在您的部分中没有看到 jQuery 的脚本标签

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.19.min.js" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.unobtrusive.min.js")" type="text/javascript"></script>   
<script src="../../Scripts/jquery-ui.unobtrusive-0.5.0.min.js" type="text/javascript"></script>

试试这个...

从 CDN 获取 jQuery 和 jQuery UI

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js" type="text/javascript"></script>

那么这三个需要按这个顺序排列。并始终@Url.Content(...)用于您的脚本

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
于 2012-06-08T14:39:53.657 回答
1

您可能希望ViewModel为该创建屏幕创建一个(除非我错了,您使用的字段比模型中的字段多,否则您也可以在每个字段Model上接收实例Controller),所以您通过方法上的单个对象(实例)CreateReservation。这将使您的事情变得更容易,并且您将使用正确接收所有值@Html.TextBoxFor

此外,为了检查您收到的内容,Controller您可以添加FormCollection类型参数。有了这个,您将能够看到正在发送到的每个字段Controller

编辑:

根据上面的文字检查一切正常并添加(如果还没有)字段的DataAnnotations,您将能够使用javascript验证;你可以查看这篇文章或这篇更深入的文章

于 2012-06-23T17:31:01.700 回答