English 中文(简体)
未经核实的确认,在无法宣布的期限内不发射
原标题:unobtrusive validation not firing on nullable date field

我有一份埃德基观点,有一份选编清单和一对二的盒子,这些箱子为可取消的田地生产。 如果没有日期,就没有进行验证,这是正确的。

然而,如果我投入一个无效的日期,验证工作不会发现这一点,并进而采取行动。 我如何在日期领域进行验证?

我的模式:

public class CaseEmployer
{
    public int Id { get; set; }
    public int CaseId { get; set; }
    public Company Company { get; set; }
    [Display(Name = "Start Date")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode=true)]
    public DateTime? StartDate { get; set; }
    [Display(Name = "End Date")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? EndDate { get; set; }
    public int? UserId { get; set; }
    public DateTime? TimeStamp { get; set; }

}

我的看法:

@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>Employer</legend>
        @Html.HiddenFor(model => model.Id)
        @Html.HiddenFor(model => model.CaseId)

    <div class="editor-label">
        @Html.Label("Company:")
    </div>
    <div class="editor-field">
        <select name="companySelect" id="companySelect">
            @foreach (var company in ViewBag.CompanyOptions)
            {
                    <option value="@company.Key">@company.Value</option>
            }
        </select>

    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.StartDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.StartDate)
        @Html.ValidationMessageFor(model => model.StartDate)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.EndDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.EndDate)
        @Html.ValidationMessageFor(model => model.EndDate)
    </div>

    <p>
        <input type="submit" value="Update" />
        <input type="button" value="Cancel" onclick="javascript:window.close()" />
        <input type="button" value="Delete" onclick="javascript:confirmDeleteCaseEmployer()" />
    </p>
</fieldset>

** 本文件迟交。 Edit**

I added "Required" attributes to the class to test if the validation would trigger, if I submitted the form without StartDate and EndDate, and the validation does trigger with those attributes.

谢谢!

问题回答

如果你打算对日间田进行验证,你很可能想研究一种固定的抑郁症,以帮助客户一方不言而喻的 Java本验证预期的投入。 你的陈代谢特性只影响验证,只是显示时间价值的方式。

你们的法典显示,在控制者采取行动之前,基本上没有任何验证。 由于更新模型无法将你的一些投入转换成一个时值,你重新出现模式转换错误。





相关问题
Using jquery to get a partial view and updating the UI

I need to render a partial view (returned from the controller) to show some customer summary details. This will need to happen when the user clicks on a button. In the the mean time the user can ...

MVC 2 / MVC 3 / MVC 4

MVC 2 我们可以轻松地创造领域。 现在,我的问题涉及nes地区(地区内)。

Asp.Net MVC 2 - Changing the PropertyValueRequired string

Using a resx file in the App_GlobalResources directory, I ve been able to change the default message for the PropertyValueInvalid string of the model validators. But it doesn t work to translate the ...

ASP.NET MVC 3 - What features do you want to see? [closed]

I know a bunch of people that are really enjoying the improvements that ASP.NET MVC 2 made over the first release. I have just started to migrate our MVC 1 project over and so far areas has totally ...

热门标签