2017-02-02 1 views
0

데이터베이스없이 DropDownList에 항목을 추가하려고합니다. 그러나 출력에만 "Testing ..."이 표시됩니다.ASP.NET MVC DropDownList를 표시하는 방법

내 모델 클래스.

public class DropDownListData 
{ 
    public DropDownListData() 
    { 
     City = new List<SelectListItem> { }; 
    } 
    public List<SelectListItem> City; 
} 

컨트롤러

public ActionResult Travel() 
{ 
    DropDownListData ddld = new DropDownListData(); 

    ddld.City = new List<SelectListItem> 
    { 
     new SelectListItem {Value = "Paris" ,Text = "Paris"}, 
     new SelectListItem {Value = "Moscow",Text = "Moscow"}, 
     new SelectListItem {Value = "Yerevan" ,Text = "Yerevan" } 
    }; 
    return View(ddld); 
} 

보기

@model ASP.NET.Test.Models.DropDownListData 

@{ 
    Layout = null; 
} 

Testing... 
@{ 
    Html.DropDownList("series", Model.City, "Choose City"); 
} 

내가 DropDownList로 표시되지 않는 이유는. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

@Html.DropDownList("series", Model.City, "Choose City") 

대신 :

그냥 MvcHtmlString으로 아무것도하지 않고, 드롭 다운리스트 함수를 호출 했는가
@{ 
    Html.DropDownList("series", Model.City, "Choose City"); 
} 

반환

답변

2

다음 코드를 사용하여 직접 출력 할 DropDownList로 :

아래의 예에 의해 입증 된 바와 같이 현재 사용이 일반적으로 "코드 블록"의 기능을 것입니다
@Html.DropDownList("series", Model.City, "Choose City"); 

:

@{ 
    // This code will be executed and can be used to set variables, etc. 
    var answer = 42; 
} 

<!-- This is an example of outputting your value --> 
<p> 
    The answer to the question is <b>@answer</b>. 
</p> 

그래서 현재 코드는 단순히 Html.DropDownList() 메서드를 호출하지만 실제로는 사용하거나 출력하지 않습니다. 그러나 문자 앞에 @ 문자를 붙이면 해당 문자가 표현식으로 처리되어 그에 맞게 출력됩니다.

1

당신은 사용해야합니다. ReSharper는 실제로 반환 값이 사용되지 않는다는 경고를 내게줍니다. 예상대로

관련 문제