Having problems with MVC 3 - sending parameters from one controller toanother
A friend and i are trying to make a site, where, a user can create/edit/delete countries, and by clicking on a link next to a country takes the user to a list of the zipcodes in that country. we kinda fail in this, so we hope someone can help us figure out what we are doing wrong.
our code is as follows:
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 of the view 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());
}
}
}
Index.cshtml of the view 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>
开发者_StackOverflow中文版 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>
You should be using RedirectToAction, not Redirect.
public ActionResult GåTilPostdistrikter(int id)
{
return RedirectToAction( "index", "Postdistrikter", new { id = id } );
}
精彩评论