2016-04-11 10 views
0

Ich möchte Redirect Details eines bestimmten Elements nach dem Klick auf ist Name auf der Homepage. Wie kann ich das machen?Wie Parameter von Teilansicht an Controller-Klasse in Mvc übergeben werden 4

Dies ist home controller meines Projekts:

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


    } 
} 

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

Dies ist 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> 

Antwort

0

Sie verwenden von

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

verwendet this overload und Hinzufügen der item.PID als htmlAttribute, kein Routenwert. Sie müssen this overload verwenden, so dass Sie Code

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

wow sein wird ..... danken youuuuuuuu. es ist workinggggg – Oshink

+0

Ich möchte wissen, was der Grund für das Hinzufügen von null ist? Was ist ein Routenwert? – Oshink

+0

Das Hinzufügen von null als letzten Parameter bedeutet, dass Sie die richtige Überladung von 'ActionLink()' aufrufen (es übergibt 'null' an den' htmlAttributes' Parameter, aber Sie können auch 'null' durch (say)' new {@class = "ersetzen myclass "}', die 'class =" myclass "' zum html hinzufügt. Schlage vor, dass du etwas über das Routing in MVC machst. Wenn du die Grundlagen des Routings nicht verstehst, wirst du viele Probleme haben, deine zu entwickeln App –

Verwandte Themen