2017-10-04 29 views
0

我剛剛開始ASP.NET的學校,我似乎無法解決這個問題。 在控制器中,我創建了一個(現在)硬編碼條目列表。 這些條目將顯示在視圖中,並且在視圖中您可以單擊其中一個條目。 我想要發生的是,當您單擊其中一個條目時,會顯示一個新視圖,其中顯示聯繫人的詳細信息(到目前爲止,只顯示電話號碼&名稱)。 有誰知道我可以做到這一點?如何從其他方法訪問列表?

控制器:

// GET: 
public ActionResult 
{ 
List<Contact> contactList = new List<Contact> { }; 
contactList.Add(new Contact("John", 10)); 
contactList.Add(new Contact("Joe", 20)); 
contactList.Add(new Contact("Jake", 30)); 

return View(contactList); 
} 

// GET: Contact/Details/5 
public ActionResult Details(int id) 
{ 
    Contact contact = contactList.Find(id); 
    return View(contact); 
} 

索引視圖:

@using WhatsUp.Models; 
@model List<Contact> 

@{ 
ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 
<ul> 
@foreach (Contact contact in Model) 
{ 
<a href="../Contact/Details/@contact.phoneNumber"><li>@contact.name @contact.phoneNumber</li></a> 
} 
</ul> 

聯繫方式查看

@using WhatsUp.Controllers 
@using WhatsUp.Models 
@model Contact 

@{ 
    ViewBag.Title = "Details"; 
} 

<h2>Contact Details</h2> 
<p>@Model.name</p> 

回答

0

聯繫人列表必須是你的指數法之外。

控制器

private static List<Contact> contactList = new List<Contact> 
{ 
    new Contact(1, "John", 10), 
    new Contact(2, "Joe", 20), 
    new Contact(3, "Jake", 30) 
}; 

public ActionResult Index() 
{ 
    return View(contactList); 
} 

// GET: Contact/Details/5 
public ActionResult Details(int id) 
{ 
    Contact contact = contactList.FirstOrDefault(x => x.Id == id); 
    return View(contact); 
} 

您可以使用方法@ Html.ActionLink創建鏈接到您的詳細信息頁面

指數

@using WhatsUp.Models; 
@model List<Contact> 

@{ 
ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 
<ul> 
@foreach (Contact contact in Model) 
{ 
<li>@Html.ActionLink(contact.name + "," + contact.phoneNumber, "Details", new { id = contact.id })</li> 
} 
</ul> 

詳細

鏈接
@using WhatsUp.Controllers 
@using WhatsUp.Models 
@model Contact 

@{ 
    ViewBag.Title = "Details"; 
} 

<h2>Contact Details</h2> 
<p>@Model.name</p> 
+0

配售名單索引方法的外界信息的工作,謝謝! :d –

0

基本上是:你CA不是。該列表僅在方法範圍內存在。

控制器而不是創建屬性:

List<Contact> contactList = new List<Contact> 
{ 
    new Contact("John", 10), 
    new Contact("Joe", 20), 
    new Contact("Jake", 30) 
}; 

然後修改您的索引方法:

public ActionResult Index 
{ 
    return View(contactList); 
} 
0

這裏我認爲你應該做的。

創建一個新的視圖模型對於指數

public class ListModel{ 
    public List<ListDataaddress { get; set; } 
} 

public class ListData{ 

    public int key { get; set; } 
    public string Value { get; set; } 

} 

控制器動作應該是這樣:

public ActionResult DestinationList(string searchData){ 

    ListModel objList= new ListModel(); 
    ListData obj = new ListData(); 
    obj.key=1; 
    ListData.Value="First Item"; 
    objList.add(obj); 
    return View(objList); 
    } 

索引視圖:

@model YOURPROJECT.Models.ListModel 

      @foreach (var item in Model.placeInfoList) 
      { 
      <p <a href="@Url.Action("ControllerName", "ActionName", new {id = item.key})">item.Value</a></p 
      } 

詳細操作:

public ActionResult Details(int id) { 

// Use Id To bind View for details (New view model you should use.) 

} 

詳情查看:您可以編寫一個基於你需要顯示在視圖