Asp.net mvc 在整个ASP.NET MVC视图中保留ID

Asp.net mvc 在整个ASP.NET MVC视图中保留ID,asp.net-mvc,jquery,razor,asp.net-ajax,partial-views,Asp.net Mvc,Jquery,Razor,Asp.net Ajax,Partial Views,基本上,我有一个图像上传控制器,我在页面中插入该控制器,如下所示:- <div id='imageList'> <h2>Upload Image(s)</h2> @{ if (Model != null) { Html.RenderPartial("~/Views/File/ImageUpload.cshtml", new MvcCommons.ViewModels.ImageMo

基本上,我有一个图像上传控制器,我在页面中插入该控制器,如下所示:-

    <div id='imageList'>
    <h2>Upload Image(s)</h2>
    @{
        if (Model != null)
        {
            Html.RenderPartial("~/Views/File/ImageUpload.cshtml", new MvcCommons.ViewModels.ImageModel(Model.Project.ProjectID));
        }  
        else
        {
            Html.RenderPartial("~/Views/File/ImageUpload.cshtml", new MvcCommons.ViewModels.ImageModel(0));
        }
    }
</div>
这又导致ImageUploadView.cshtml:-

<table>
@if (Model != null)
{
  foreach (var item in Model)
  {
     <tr>
        <td>
          <img src= "@Url.Content("/Uploads/" + item.FileName)" />
        </td>
        <td>
          @Html.DisplayFor(modelItem => item.Description)
        </td>
    </tr>    
 }
}
</table>

@using (Html.BeginForm("Save", "File", new { ProjectID = Model.ProjectID }, 
       FormMethod.Post, new { enctype = "multipart/form-data" }))

{
    <input type="file" name="file" />
    <input type="submit" value="submit" /> <br />
    <input type="text" name="description" /> 
}
使用ProjectID正确填充,但是,当我上载图像时,ProjectID将丢失,并变为零。有什么方法可以让我第二次坚持这个项目吗

谢谢你的帮助和时间

***************更新************************** 上传后,FileController内部的操作如下:-

        public ActionResult Save(int ProjectID)
    {
        foreach (string name in Request.Files)
        {
            var file = Request.Files[name];

            string fileName = System.IO.Path.GetFileName(file.FileName);
            Image image = new Image(fileName, Request["description"]);

            ImageModel model = new ImageModel();
            model.Populate();
            model.Add(image, file);
        }
        return RedirectToAction("ImageUpload");
    }

您可以将
projectId
作为路由值从
RedirectToAction
传递。您应该更改
ImageUpload
操作以接受
projectId

public ActionResult Save(int projectId)
{
  ....
  return RedirectToAction("ImageUpload", new { projectId = projectId });
}

public ActionResult ImageUpload(int projectId)
{
   var model = .. get the model from db based on projectId
   return View("view name", model);   
}

我可以想到的一件事是在global.asax.Hi mazhar中定义一个save/file/id路由,你能给我举个例子或给我指一些教程吗。在上传图像之前从未这样做过。您是否仍显示相同的视图?您能发布一些保存/文件操作的代码吗?@Mark谢谢您的编辑!我已经用Save操作更新了代码,请发布ImageUpload操作,谢谢Mark,我会尝试一下,并尽快通知您!您好,Mark,这并不完全有效,因为在ImageUpload.cshtml中,第二次,ProjectID是0。因此,当我创建项目时,ID是正确的,但是当我上传并按submit时,ProjectID会再次重置为0,所以看起来并不是keptR u一直在ImageUpload操作的返回视图()中发送模型?我认为,由于模型为空,您第二次将projectid设置为“0”。您好,我正在考虑使用TempData[“projectid”],实际上已经尝试过了,它可以正常工作。使用TempData可以吗?我的意思是它没有您的解决方案优雅,但我认为它更容易使用。回答您的问题,我看到的问题是RenderPartial没有通过FileController中的ImageUpload操作,因此,我没有保留ProjectID
        public ActionResult Save(int ProjectID)
    {
        foreach (string name in Request.Files)
        {
            var file = Request.Files[name];

            string fileName = System.IO.Path.GetFileName(file.FileName);
            Image image = new Image(fileName, Request["description"]);

            ImageModel model = new ImageModel();
            model.Populate();
            model.Add(image, file);
        }
        return RedirectToAction("ImageUpload");
    }
public ActionResult Save(int projectId)
{
  ....
  return RedirectToAction("ImageUpload", new { projectId = projectId });
}

public ActionResult ImageUpload(int projectId)
{
   var model = .. get the model from db based on projectId
   return View("view name", model);   
}