2011-11-10 54 views
1

大家好,我试图在不同的页面之间传递信息,但我不知道如何。如何在不同的网页之间传递信息

我有这种形式与Html.ActionLink内

<% using (Html.BeginForm("Save", "Envi")) 
    {%> 
     <%: Html.ValidationSummary(true)%> 

      <div class="editor-label"> 
       <%: Html.Label("Description:")%> 
      </div> 
      <div class="editor-field"> 
       <%: Html.TextBox("info", lp.Description)%> 
    ... 
      <div> 
       <%: Html.ActionLink("Change Image", "ChangeImg", "Envi", new {id=lp}, new {id="cambio"})%> 
    ... 
      <p> 
       <input type="submit" value="Save" name="<%= lp.Id %>"/> 
      </p> 
    <% } %> 
<% } %> 

当我点击我展示Html.ActionLink其他页面(与对话的fancybox),我选择一个图像。

我想将表单中的所有数据传递到此页面。现在,当我再次显示表单时,我有新数据,没有旧数据。 我该怎么做?

谢谢。

+0

再次调用此视图的控制器需要将正确的模型传递给它,其中包含要显示的数据。如果没有足够的信息,还可以发布控制器代码以进行“Save”和“ChangeImg”视图。 (总共应该有4个控制器方法,2个HttpGet和2个HttpPost) – Dave

回答

0

建议您使用TempData字典。这将仅适用于下一个请求。

从MSDN引用:

的动作方法可以将数据存储在控制器的TempDataDictionary 对象它调用控制器的RedirectToAction方法之前 调用下一个动作。 TempData属性值存储在会话状态 中。在设置了TempDataDictionary值之后调用的任何操作方法都可以从对象中获取值,然后处理或显示它们。 TempData的值会一直存在,直到它读取到 或者直到会话超时。以这种 方式持久TempData可实现诸如重定向之类的场景,因为TempData中的值可在单个请求之外使用。

希望这给你的答案。

0

理想情况下,我认为表单应该提交一个单一的动作。

所以控制器可以是这样的:

public class HomeController : Controller 
{ 
    public ViewResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Index(ItemModel itemModel, string submit) 
    { 
     //I'm not sure why I need this but the fields display with empty results on my machine otherwise 
     ModelState.Clear(); 

     if (submit == "edit") 
     { 
      this.TempData.Add("item", itemModel); 
      return View("ChangeImage", new ImageModel { ImageName = itemModel.ImageName }); 
     } 
     else 
     { 
      //perform save here 
      return RedirectToAction("ViewAfterSavePerformed"); 
     } 
    } 

    [HttpPost] 
    public ViewResult Image(ImageModel imageModel) 
    { 
     ItemModel itemModel = (ItemModel)this.TempData["item"]; 
     itemModel.ImageName = imageModel.ImageName; 
     return View("Index", itemModel); 
    } 
} 

用下面的视图模型:

public class ItemModel 
{ 
    public string Description { get; set; } 

    public string ImageName { get; set; } 
} 

public class ImageModel 
{ 
    public string ImageName { get; set; } 
} 

而下面的观点:

指数:

<h2>Index</h2> 

@using (Html.BeginForm()) 
{ 
    <p>Description: @Html.EditorFor(m => m.Description)</p> 
    <p>Image: @Html.EditorFor(m => m.ImageName)</p> 
    <input type="submit" name="submit" value="edit" /> 
    <input type="submit" name="submit" value="save" /> 
} 

更改图片:

<h2>ChangeImage</h2> 

@using (Html.BeginForm("Image", "Home")) 
{ 
    <p>Image: @Html.EditorFor(m => m.ImageName)</p> 

    <input type="submit" name="submit" value="save image" /> 
} 

但愿这应该可以感知,即使我用剃刀语法。