控制器代碼: public string ValidateCarID(string carid) { string result; Car car = db.Car.Find(carid); if(car==null) { result = "false"; //沒有找到 } else { resu ...
控制器代碼:
public string ValidateCarID(string carid)
{
string result;
Car car = db.Car.Find(carid);
if(car==null)
{
result = "false"; //沒有找到
}
else
{
result = "true";
}
return result;
}
ajax部分:
<script type="text/javascript">
$(document).ready(function () {
$("#carID").change(function () {
var caridNode = $("#carID").val();
$.ajax({
type: "post",
url: "../../Cars/ValidateCarID?carid=" + caridNode,
dataType:"text",
success: function (result) {
if (result == "true") {
$.ligerDialog.error("該車輛已註冊");
}
}
});
});
});
</script>
視圖控制項部分:
<tr>
<td>
@Html.LabelFor(model => model.CarID, htmlAttributes: new { @class = "control-label col-md-2" })
</td>
<td>
@Html.EditorFor(model => model.CarID, new { htmlAttributes = new { @class = "form-control",id="carID"} })//這個是需要判斷的控制項
@Html.ValidationMessageFor(model => model.CarID, "", new { @class = "text-danger" })
</td>
</tr>