Ajax表單確實更新了選定的div,但不是隻是重新加載該div中的局部視圖,而是將整個頁面內容插入到該div中。MVC使用Ajax.BeginForm更新局部視圖
.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>
您的'Ajax.BeginForm'不應該針對'GetFilesData'操作嗎? – asymptoticFault
謝謝!成功了! –