【问题标题】:how to pass parameter from partial View to controller class in mvc 4如何将参数从部分视图传递到mvc 4中的控制器类
【发布时间】:2016-04-11 20:17:44
【问题描述】:

单击主页中的名称后,我想重定向特定项目的详细信息。我该怎么做?

这是我项目的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>

【问题讨论】:

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


    【解决方案1】:

    你使用的

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

    【讨论】:

    • 想知道加null的原因是什么?什么是路由值?
    • 添加 null 作为最后一个参数意味着您调用了 ActionLink() 的正确重载(它将 null 传递给 htmlAttributes 参数,但您也可以将 null 替换为(比如)@987654331 @ 会将class="myclass" 添加到 html 中。建议您对 MVC 中的路由进行一些研究。如果您不了解路由的基础知识,那么在开发您的应用程序时将会遇到很多问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 2013-09-07
    • 2018-07-10
    • 1970-01-01
    • 2013-05-20
    相关资源
    最近更新 更多