2010-07-15 3 views
0

내 URL이 올바르게 작동하지만 정확한 컨트롤을 사용할 수는 있지만 어떻게하면 URL에서 상태 이름을 볼 수 있습니까?MVC2에서 쿼리 문자열 값을 읽고 전달하는 적절한 방법은 무엇입니까?

내 URL : http://localhost:10860/Listings/Arizona/page1

내보기 :

> "%>

<h2>Test BY STATE</h2> 

<% 
    LOTW.Models.ListingRepository dr = new LOTW.Models.ListingRepository(); 
    ListViewListings.DataSource = dr.GetByStateName(???? I can hard code "Arizona" and this works???????); // how do i grab the 'Arizona' from the url? Reqquest.Querystring doesn't work? 
    ListViewListings.DataBind(); 
    %> 

<%--Define the table headers to work with the tablesorter--%> 
    <asp:ListView runat="server" ID="ListViewListings"> 
     <LayoutTemplate> 
      <table id="ListViewListings" class="tablesorter"> 
       <thead> 
        <tr>..... 

답변

0
내가보기에 많은 코드가 컨트롤러를 사용하지 왜 읽을 것을 피하기 것

. Querystring을 선택하고 ViewData를 사용하여 컨트롤러에 값을 전달하십시오.

컨트롤러

Function Index() As ActionResult 
    ''# however you access your repository 
    ViewData("StateName") = dr.GetByStateName(Request.QueryString("TheState")) 
End Function 

마크 업

<% For Each item In ViewData("StateName") %> 
     <li><%: item.State %></li> 
<% Next%> 
+0

야 내가 좋아하는 당신 아이디어가 더 좋다 .... 내가 방금 찾은 것보다 ... request.querystring 대신 [...] ... RouteData.Values ​​[ "stateName"]. ToString() ... 그러나 나는 대신 제어기에 넣을 것입니다 ... thanks x 10 – Bryant

+0

마크 업에서 ''컨트롤을 사용하지 않으려면 최대한 노력하십시오. 대신 MVC의 진정으로 무국적 인 성격을 포용하고 가능한 한 일반적인 HTML을 사용하십시오. 내 Views에서'DataSources' 또는'' 컨트롤을 사용하는 이유를 아직 찾지 못했습니다. –

0

비트 아래 정말보기

<% 
LOTW.Models.ListingRepository dr = new LOTW.Models.ListingRepository(); 
ListViewListings.DataSource = dr.GetByStateName(???? I can hard code "Arizona" and this works???????); // how do i grab the 'Arizona' from the url? Reqquest.Querystring doesn't work? 
ListViewListings.DataBind(); 
%> 

그것의 일부는 정말 컨트롤러 액션 메소드에 있어야한다에 속하지 않습니다.

class HomeController { 
public ActionResult Index(string state) { 
    LOTW.Models.ListingRepository dr = new LOTW.Models.ListingRepository(); 
    var list = dr.GetByStateName(state); // how do i grab the 'Arizona' from the url? Reqquest.Querystring doesn't work? 

    return View(list); 
} 
} 

조치 방법의 매개 변수 state은 URL에서 가져옵니다. 당신이 당신의 경로를 설정하는 방법에 따라, mysite.com/home/NY 것 중 하나 또는 그 다음 mysite.com/home/?state=NY

은보기에 :

<% 
ListViewListings.DataSource = Model; 
ListViewListings.DataBind(); 
%> 
+0

쿨, 나중에 클리너 감각 ... 고마워. – Bryant

관련 문제