遇到MVC 3问题 - 将参数从一个控制器发送到另一个控制器

时间:2011-03-13 15:46:16

标签: asp.net-mvc asp.net-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);
    }
    ...
}
}

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); } ... } }

视图ISOer 的

Index.cshtml

@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 @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>

using SkarpSpedition.Models;

视图Postdistrikter

Index.cshtml



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());
    }


}
}

1 个答案:

答案 0 :(得分:2)

您应该使用RedirectToAction,而不是Redirect。

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