使用条件语句要求输入的最佳方法

时间:2018-07-11 15:22:39

标签: model-view-controller

我正在开发一个停车应用程序,并要求用户输入汽车数据。如果状态不是“ WA”,则希望他们输入VIN。是在客户端或服务器上执行此操作的最佳位置?我已经在模型中尝试了Data Annotations Requiredif,但似乎只在需要检查字符串值的地方检查null。我是否应该考虑一下条件检查,您能推荐最好的方法吗?我在这方面变得越来越好,但仍然时不时遇到障碍。谢谢!

这是我的一些代码。在视图中:

@using (Html.BeginForm("SubmitVehicle", "Home", FormMethod.Post, new { 
enctype = "multipart/form-data" }))
{
    <div class="form-group">
    @Html.Label("ID")
    @Html.TextBoxFor(m => m.PersonID, new { @class = "form-control", placeholder = "Student or Staff ID number", autofocus = "autofocus" })
    @Html.ValidationMessageFor(m => m.PersonID, "", new { @class = "text-danger" })
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Plate)
    @Html.TextBoxFor(m => m.Plate, new { @class = "form-control" })
    @Html.ValidationMessageFor(m => m.Plate, "", new { @class = "text-danger" })
</div>
<div class="form-group">
    @Html.LabelFor(m => m.State)
    @Html.DropDownListFor(x => x.State, 
    Parking.Models.DdlData.GetStatesList(), new { @class = "form-control" })
    @Html.ValidationMessageFor(m => m.State, "", new { @class = "text-danger" })
</div>
<div class="form-group">
    @Html.LabelFor(m => m.VIN)
    @Html.TextBoxFor(m => m.VIN, new { @class = "form-control", placeholder = "VIN required if out of state vehicle" })
    @Html.ValidationMessageFor(m => m.VIN)
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Make)
    @Html.TextBoxFor(m => m.Make, new { @class = "form-control", placeholder = "e.g. Toyota" })
    @Html.ValidationMessageFor(m => m.Make, "", new { @class = "text-danger" })
</div>
.... etc.
<button type="submit" class="btn btn-primary btn-lg">Add Vehicle</button>

0 个答案:

没有答案