2011-09-20 2 views
0

필터링 할 값을 여러 개 허용하는 작업이 있습니다. 이 값은 선택 사항입니다. 기본값을 설정합니다. 기본값은 내가 입력 한 값보다 우선합니다.기본값이 값으로 전달 된 재정의입니다.

내가 패스하면 : minAge : 40, maxAge : 40. 두 값이 0

로 설정이 내 컨트롤러에 대한 지침 :

[HttpGet] 
    public ActionResult DataTableUpdate(string firstName = "", string lastName = "", int minAge = 0, int maxAge = 0, string currentState = "") 
    { 
     List<DataMember> data = DataMemberCache.GetMembers().FindAll(d => (d.FirstName.Contains(firstName)) && (d.LastName.Contains(lastName)) && (d.Age < minAge) && (d.Age > maxAge) && (d.CurrentState.Contains(currentState))); 
     return PartialView("_DataTable", data); 
    } 

답변

1

는 여기를 참조하십시오 : 예를 들어 Optional parameters in the MVC framework are handled by using nullable-type arguments for controller action methods. 을하는 방법은 쿼리 문자열의 일부로서 일을 할 수 있지만 당신이 원하는 경우 쿼리 문자열 매개 변수가 누락 된 경우 기본값은 오늘 날짜로하려면 다음 예제에서와 같은 코드를 사용할 수 있습니다

:

public ActionResult ShowArticles(DateTime? date) 
{ 
    if(!date.HasValue) 
    { 
     date = DateTime.Now; 
    } 
    // ... 
} 

그래서, 코드가이 일을 변경해야합니다

[HttpGet] 
public ActionResult DataTableUpdate(string firstName, string lastName, int? minAge, int? maxAge, string currentState) 
{ 
    firstName = firstName ?? ""; 
    lastName = lastName ?? ""; 
    minAge = minAge ?? 0; 
    maxAge = maxAge ?? 0; 
    currentState = currentState ?? ""; 
    List<DataMember> data = DataMemberCache.GetMembers().FindAll(d => (d.FirstName.Contains(firstName)) && (d.LastName.Contains(lastName)) && (d.Age < minAge) && (d.Age > maxAge) && (d.CurrentState.Contains(currentState))); 
    return PartialView("_DataTable", data); 
} 
관련 문제