剃须刀视图上的模型在表单提交时未更新


public ActionResult Index(int id, string name)
{
var model = new ITViewModel
{
Packages = _Repository.GetDeployedPackages(id)
};
return View(model);
}
[HttpPost]
public ActionResult GeneratePackage(ITViewModel model)
{
_Repository.SavePackage(model);
//Generate Zip file Package
//Get file template  in archiveStream
Response.Clear();
Response.ContentType = "application/zip";
Response.AppendHeader("content-disposition", "attachment; filename="testzipPackage");
Response.CacheControl = "Private";
Response.Cache.SetExpires(DateTime.Now.AddMinutes(3));
Response.Buffer = true;
var writeBuffer = new byte[4096];
var count = archiveStream.Read(writeBuffer, 0, writeBuffer.Length);
while (count > 0)
{
Response.OutputStream.Write(writeBuffer, 0, count);
count = archiveStream.Read(writeBuffer, 0, writeBuffer.Length);
}
model.Packages = _Repository.GetDeployedPackages(model.id) //get the correct package list with the one tht we just saved on this ActionResult
return View("Index",model);
}
//Index
@model  ITViewModel
@using (Html.BeginForm("GeneratePackage", "Integration", FormMethod.Post)
{
//some input form 
}
<table>
@foreach (var package in Model.Packages)
{
<tr>
<td>
@package.Name
</td>
</tr>
}
</table>

我能够正确下载zip文件。在调试器中,我还看到了包含新添加元素的包列表。但是帖子视图没有刷新。我的意思是索引上的表不会使用新的模型元素刷新。甚至 document.ready 也没有被调用一次 返回视图("索引",模型)被触发。

I have tried ModelState.Clear(). It didn't work.

不能从单个 HTTP 请求返回两个不同的响应。

在这里,您正在编写响应:

Response.OutputStream.Write(writeBuffer, 0, count);

之后您执行的任何操作都不会由服务器或客户端处理。

您的网络浏览器正在下载文件,而不仅仅是停留在同一页面上。这绝对是正常的。

如果你想刷新页面,你可能需要使用 JavaScript 客户端来完成。

下面是一个使用 jQuery 的小示例,假设myForm作为表单 ID:

$('#myForm').submit(function() {
setTimeout(function () {
window.location.reload();
}, 1000); // use a timeout as big as you need
});

您可能还需要将target="_blank"添加到表单标签中。

最新更新