Ajax 表单确实更新了选定的 div,但它不仅仅是在该 div 中重新加载部分视图,而是将整个页面内容插入到该 div 中。
.cshtml 文件
<fieldset>
<legend>File(s)</legend>
<div id="filesBody">
@Html.Action("Action", "Controller", new {id=Model.Id})
</div>
<br />
@using (Ajax.BeginForm("UploadFile", "Controller", null, new AjaxOptions { UpdateTargetId="filesBody"}, new { enctype = "multipart/form-data", @id = "myForm" }))
{
@Html.HiddenFor(model => model.ComplaintId)
<div>
<label for="File">Add File:</label>
<input type="file" name="FileAttachment" />
<input type="submit" value="Upload" />
@Html.ValidationMessage("FileAttachment")
</div>
}
</fieldset>
控制器
public PartialViewResult GetFilesData(long? Id)
{
Model Model = new Model(Id);
TryUpdateModel(Model);
return PartialView(Model);
}
局部视图
@model Models
<div id="reloadField">
@foreach (var ph in Model.docs)
{
///code
}
</div>