2011-03-13 75 views
0

一位朋友和我正在嘗試創建一個網站,用戶可以在其中創建/編輯/刪除國家/地區,並通過單擊國家旁邊的鏈接用戶轉到該國家的郵編列表。我們有點失敗,所以我們希望有人能幫助我們弄清楚我們做錯了什麼。MVC 3有問題 - 將參數從一個控制器發送到另一個控制器

我們的代碼如下:

ISOerController.cs

using SkarpSpedition.Models; 

using System; 
using System.Linq; 
using System.Web.Mvc; 


namespace SkarpSpedition.Controllers 
{ 
    public class ISOerController : Controller 
{ 
    ISODBContext db = new ISODBContext(); 

    public ActionResult Index() 
    { 
     var ISOer = from i in db.ISOes 
        orderby i.Sted ascending 
        select i; 
     return View(ISOer.ToList()); 
    } 
    public ActionResult GåTilPostdistrikter(int id) 
    { 
     return Redirect(@"../../Postdistrikter/" + id); 
    } 
    ... 
} 
} 

Index.cshtml視圖ISOer的

@model IEnumerable<SkarpSpedition.Models.ISO> 
@{ 
ViewBag.Title = "ISO-Koder"; 
} 

<h2> 
ISO-Kode Oversigt</h2> 
<p> 
@Html.ActionLink("Opret ny", "Create") 
</p> 
<table> 
<tr> 
    <th> 
     Sted 
    </th> 
    <th> 
     Alph2 
    </th> 
    <th> 
     Alph3 
    </th> 
    <th> 
     Numc 
    </th> 
    <th> 
    </th> 
</tr> 
@foreach (var item in Model) 
{ 
    <tr> 
     <td> 
      @item.Sted 
     </td> 
     <td> 
      @item.Alph2 
     </td> 
     <td> 
      @item.Alph3 
     </td> 
     <td> 
      @item.Numc 
     </td> 
     <td>@Html.ActionLink("Postdistrikter", "GåTilPostdistrikter", new { id = item.ID }) | 
      @Html.ActionLink("Rediger", "Edit", new { id = item.ID }) | 
      @Html.ActionLink("Slet", "Delete", new { id = item.ID }) 
     </td> 
    </tr> 
} 
</table> 

PostdistrikterController.cs using SkarpSpedition.Models;

using System; 
using System.Linq; 
using System.Web.Mvc; 

namespace SkarpSpedition.Controllers 
{ 
public class PostdistrikterController : Controller 
{ 

    PostdistriktDBContext db = new PostdistriktDBContext(); 

    public ActionResult Index(int id) 
    { 
     var Postdistrikter = from p in db.Postdistrikts 
          where p.Iso_id == id 
          orderby p.Postnummer ascending 
          select p; 

     return View(Postdistrikter.ToList()); 
    } 


} 
} 

認爲Postdistrikter

@model IEnumerable<SkarpSpedition.Models.Postdistrikt> 
@{ 
    ViewBag.Title = "Index"; 
} 
<h2> 
    Postdistrikts oversigt</h2> 
<p> 
    @Html.ActionLink("Opret ny", "Create") 
</p> 
<table> 
    <tr> 
     <th> 
      Postnummer 
     </th> 
     <th> 
      Bynavn 
     </th> 
     <th> 
      Adresse 
     </th> 
     <th> 
      Land 
     </th> 
     <th> 
     </th> 
    </tr> 
    @foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @item.Postnummer 

      </td> 
      <td> 
       @item.Bynavn 
      </td> 
      <td> 
       @item.Adresse 
      </td> 
      <td> 
       @item.Iso_id 
      </td> 
      <td> 
       @Html.ActionLink("Rediger", "Edit", new { id = item.ID }) | 
       @Html.ActionLink("Slet", "Delete", new { id = item.ID }) 
      </td> 
     </tr> 
    } 
</table> 

回答

2

您應該使用RedirectToAction的Index.cshtml,而不是重定向。

public ActionResult GåTilPostdistrikter(int id) 
{ 
    return RedirectToAction("index", "Postdistrikter", new { id = id }); 
} 
相關問題