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

Model on the razor view is not getting updated on form submit

  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 文件。在调试器中,我还看到了包含新添加元素的包列表。但是 Post 上的视图没有得到刷新。我的意思是索引上的 table 不会用新的模型元素刷新。即使 document.ready 也不会被调用一次 return 视图("Index",模型)被触发。

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

您不能 return 来自单个 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" 添加到您的表单标签中。