Asp.net MVC4中如何将参数从局部视图传递到控制器类
我想重定向的具体项目后,点击是在主页上的名称细节。我该怎么做 这是我的项目的Asp.net MVC4中如何将参数从局部视图传递到控制器类,asp.net,asp.net-mvc-4,razor,Asp.net,Asp.net Mvc 4,Razor,我想重定向的具体项目后,点击是在主页上的名称细节。我该怎么做 这是我的项目的主控制器: namespace WEB1.Controllers { public class HomeController : Controller { private projectDBEntities db = new projectDBEntities(); public ActionResult Index() { re
主控制器
:
namespace WEB1.Controllers
{
public class HomeController : Controller
{
private projectDBEntities db = new projectDBEntities();
public ActionResult Index()
{
return View();
}
public ActionResult topPlace()
{
var top_place = db.Places.OrderByDescending(a => a.place_rate).Take(6).ToList();
return PartialView("_topPlace", top_place);
}
public ActionResult topServices()
{
var top_service = db.service_provider.OrderByDescending(a => a.Sp_rate).Take(6).ToList();
return PartialView("_topService",top_service);
}
}
}
这是TDS控制器:
namespace WEB1.Controllers
{
public class TDSController : Controller
{
private projectDBEntities db = new projectDBEntities();
public ActionResult Details(int id = 0)
{
Place place = db.Places.Find(id);
if (place == null)
{
return HttpNotFound();
}
return View(place);
}
public ActionResult Index(int? page)
{
var places = db.Places.Include(p => p.city).OrderByDescending(s => s.place_rate);
if(Request.HttpMethod != "GET")
{
page = 1;
}
int pageSize = 5;
int pageNumber =(page ?? 1);
return View(places.ToPagedList(pageNumber, pageSize));
}
public ActionResult Historical(int? page)
{
var places = db.Places.Include(p => p.city).OrderByDescending(s => s.place_rate).Where(s => s.Place_type == "Historical");
if (Request.HttpMethod != "GET")
{
page = 1;
}
int pageSize = 5;
int pageNumber = (page ?? 1);
return View(places.ToPagedList(pageNumber, pageSize));
}
public ActionResult Religious(int? page)
{
var places = db.Places.Include(p => p.city).OrderByDescending(s => s.place_rate).Where(s => s.Place_type == "Religious");
if (Request.HttpMethod != "GET")
{
page = 1;
}
int pageSize = 5;
int pageNumber = (page ?? 1);
return View(places.ToPagedList(pageNumber, pageSize));
}
public ActionResult Scenic(int? page)
{
var places = db.Places.Include(p => p.city).OrderByDescending(s => s.place_rate).Where(s => s.Place_type == "Scenic");
if (Request.HttpMethod != "GET")
{
page = 1;
}
int pageSize = 5;
int pageNumber = (page ?? 1);
return View(places.ToPagedList(pageNumber, pageSize));
}
public ActionResult Educational(int? page)
{
var places = db.Places.Include(p => p.city).OrderByDescending(s => s.place_rate).Where(s => s.Place_type == "Educational");
if (Request.HttpMethod != "GET")
{
page = 1;
}
int pageSize = 5;
int pageNumber = (page ?? 1);
return View(places.ToPagedList(pageNumber, pageSize));
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
这是顶部零件视图:
@model IEnumerable<WEB1.Models.Place>
<div class="homebody">
<h3>Most Populer Places</h3>
@foreach (var item in Model)
{
<div class="span4">
<div class="item1">
<p class="name">@Html.ActionLink(item.Place_name, "Details", "TDS", new { id = item.PID })</p>
<p class="cattype">@Html.DisplayFor(modelItem => item.Place_location)</p>
<p>@Html.DisplayFor(modelItem => item.city.Cityname)</p>
<p>@Html.DisplayFor(modelItem => item.place_rate)</p>
</div>
</div>
}
</div>
@model IEnumerable
人口最多的地方
@foreach(模型中的var项目)
{
@Html.ActionLink(item.Place\u name,“Details”,“TDS”,new{id=item.PID})
@Html.DisplayFor(modelItem=>item.Place\u位置)
@DisplayFor(modeleItem=>item.city.Cityname)
@DisplayFor(modelItem=>item.place\u rate)
}
您使用
@Html.ActionLink(item.Place_name, "Details", "TDS", new { id = item.PID })
正在使用并添加项.PID
作为htmlAttribute
,而不是路由值。您需要使用,这样您的代码将
@Html.ActionLink(item.Place_name, "Details", "TDS", new { id = item.PID }, null)
我想知道添加null的原因是什么?什么是路由值?添加null作为最后一个参数意味着您调用了正确的
ActionLink()
(它将null
传递给htmlAttributes
参数,但您也可以将null
替换为(比如)新的{@class=“myclass”},这将添加class=“myclass”
到html。建议您在MVC中研究路由。如果您不了解路由的基本知识,那么在开发应用程序时会遇到很多问题。