我的表单验证遇到了麻烦。由于某种原因,表单忽略了我的模型中的任何[必需]属性。
型号:(为了便于阅读而拆了下来)
public class NotEmployeesModel
{
public NotEmployeesModel()
{
DetailModel = new NotEmployeesDetailModel();
}
public NotEmployeesDetailModel DetailModel { get; set; }
}
public class NotEmployeesDetailModel
{
public NotEmployeesDetailModel()
{
}
public NotEmployeesDocumentModel DocumentModel { get; set; }
}
public class NotEmployeesDocumentModel
{
public NotEmployeesDocumentModel()
{
}
public NotEmployeeDocumentInputModel DocumentInput { get; set; }
public class NotEmployeeDocumentInputModel
{
public NotEmployeeDocumentInputModel()
{
}
public NotEmployeeDocumentInputModel(int notEmployeeId)
{
NotEmployeeId = notEmployeeId;
}
public int NotEmployeeId { get; set; }
public int SelectedDocumentType { get; set; }
[Required(ErrorMessageResourceType = typeof(ErrorMessages), ErrorMessageResourceName = "Star")]
public string Description { get; set; }
[Required(ErrorMessageResourceType = typeof(ErrorMessages), ErrorMessageResourceName = "Star")]
public HttpPostedFileBase File { get; set; }
}
}
对于每个视图或部分我有一个单独的模型类。
形式:
@model NotEmployeesDocumentModel
@using (Html.BeginForm("AddNotEmployeeDocument", "Home", FormMethod.Post, new { id = "form-add-document", enctype = "multipart/form-data" }))
{
@Html.HiddenFor(x => x.DocumentInput.NotEmployeeId)
<table class="table-output">
<thead>
<tr>
<td>@Html.Label("Sort", Labels.Sort)</td>
<td>@Html.Label("Description", Labels.Description)</td>
<td>@Html.Label("Type", Labels.Type)</td>
<td class="text-align-right"></td>
</tr>
</thead>
<tbody>
<tr>
<td>
<i class="fa fa-plus-square cursor-pointer add-document"></i>
<input type="file" id="DocumentInput_File" name="DocumentInput.File" required="required" />
@Html.ValidationMessageFor(x => x.DocumentInput.File)
</td>
<td>
@Html.TextBoxFor(x => x.DocumentInput.Description, new { @class = "width300px hardware-setup-input", placeholder = "Vul hier een omschrijving in..." })
@Html.ValidationMessageFor(x => x.DocumentInput.Description)
</td>
<td>
@Html.DropDownListFor(x => x.DocumentInput.SelectedDocumentType, Model.DocumentTypes, "--- Maak een keuze ---")
</td>
<td class="text-align-right">
<span id="btn-add-document" class="button-org">@Labels.Save</span>
</td>
</tr>
</tbody>
</table>
}
我的页面结构:查看/部分/部分(这是我的表格)
JS:
$("#btn-add-document").on("click", function () {
var frm = $("#form-add-document");
if (frm.valid()) {
frm.ajaxSubmit({
dataType: "html",
success: function (responseText) {
$("#document-container").html(responseText);
$("#DocumentInput_Description").text("");
$("#DocumentInput_SelectedDocumentType").val("");
loadDocumentPartial();
}
});
}
});
我正在使用malsup的jQuery Form插件通过ajax提交我的表单。
控制器:
[HttpPost]
public ActionResult AddNotEmployeeDocument(NotEmployeesDocumentModel input)
{
// do some code
}
正如你在ActionResult的参数中看到的那样,我不能像往常一样放置NotEmployeesDocumentInputModel,但我不得不使用父类。
我不知道我做错了什么。这是我第一次遇到这样的问题。