如何将参数从部分View传递到mvc 4中的控制器类

时间:2016-04-11 20:17:45

标签: asp.net asp.net-mvc-4 razor

我希望点击后的特定项目的重定向详细信息是主页中的名称。我怎么能这样做?

这是我项目的home controller

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 controller

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

这是top places partail view

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

1 个答案:

答案 0 :(得分:0)

您使用

@Html.ActionLink(item.Place_name, "Details", "TDS", new { id = item.PID })

正在使用this overload并将item.PID添加为htmlAttribute,而非路由值。您需要使用this overload,以便您的代码为

@Html.ActionLink(item.Place_name, "Details", "TDS", new { id = item.PID }, null)