2013-03-13 19 views
2

我使用asp.net的MVC 4,我有以下情形動態Actionlinks

Cities   Places  Events 
------   ------------------ 
City 1   |     
City 2   | 
       | 

在數據庫中的城市的左側導航(城市)名單的所有。地方和事件也是行動方法的鏈接。

<li>@Html.ActionLink("Places", "Places", null, new{id="placeslink"})</li> 
<li>@Html.ActionLink("Events", "Events", null, new{id="eventslink"})</li> 

我異步加載使用下面的腳本(jQuery的)

$('#placeslink').click(function (event) { 
    event.preventDefault(); 

    var url = $(this).attr('href'); 
    $('#content').html(ajax_load).load(url); 
}); 

$('#eventslink').click(function (event) { 
    event.preventDefault(); 

    var url = $(this).attr('href'); 
    $('#content').html(ajax_load).load(url); 
}); 

它的地點和事件是工作的罰款,並填充所有的地方(不特定的城市)和活動頁面上的數據庫時地點和事件鏈接被點擊。

現在我想要實現的是,當用戶在查看地點時單擊城市時,只顯示該城市中的地點,如果選擇了事件,則同一城市鏈接應顯示該城市中的事件。 如果選擇城市(例如城市1)並且用戶點擊地點,則顯示所選城市中的地點,並且如果她單擊事件,則顯示所選城市的事件。

我有以下操作方法

public ActionResult Places() 
{ 
    if (Request.IsAjaxRequest()) 
    { 
    .... 
    return PartialView(model);    

    } 
    return View(); 
} 

它很混亂,我想不出一個單一的方式如何爲城市,地點和事件相應的鏈接,並達到上述效果。

回答

1

這給人一種嘗試,我會做這樣的

public class PlacesAndEventsViewModel 
{ 
    public string LocationOption { get; set; } //places or events 

    public List<Place> Places { get; set; } 

    public List<Event> Events { get; set; } 

    public int? CityID { get; set; } 
} 

視圖模型和我的控制器

//this is get 
public ActionResult ShowLocations() 
{ 
    var model = new PlacesAndEventsViewModel(); 

    model.CityID = null; //or any default value 
    model.LocationOption = "places"; //or any default value 
    model.Places = new List<Place>(); //or GetAllPlacesFromDB(); 
    //You can do the same for events but I think you need one at a time 

    return View("ViewPlaces", model); 
} 

[HttpPost] 
public ActionResult ShowLocations(PlacesAndEventsViewModel model) 
{ 
    if(model.LocationOption == "places") 
    { 
     model.Places = GetAllPlacesByCity(model.CityID); 
     return View("ViewPlaces", model); //All these could be partial view 
    } 
    else if(model.LocationOption == "cities") 
    { 
     model.Events = GetAllEventsByCity(model.CityID); 
     return View("ViewEvents", model); //All these could be partial view 
    } 
    else 
    { 
     return View("ViewPlaces", model); //All these could be partial view 
    } 
} 

您可能需要您的Ajax更改爲$阿賈克斯()

$.ajax({ 
    url: '@Url.Action("ShowLocation"), 
    data: { LocationOption: '@Model.LocationOption', CityID: @Model.CityID } 
    }); 
+0

謝謝..讓我試試這個 – ZedBee 2013-03-13 10:02:42