2010-07-28 3 views
3

쿼리 문자열의 매개 변수를 기반으로 호출되는 동작을 필터링 할 수 있는지 궁금합니다.ASP.NET MVC - 쿼리 문자열을 기반으로 호출 할 동작 필터링

예를 들어, 격자의 항목을 선택하는 라디오 단추 열이있는 격자가 있습니다. 그리드는 폼으로 싸여 있고 그리드의 맨 위에는 선택된 아이템을 편집/삭제하는 버튼이 있습니다. 편집/삭제 단추를 클릭하면 편집 및 게시물을 구별 할 수 있도록 명령 속성을 설정하기 위해 jquery 마법을 사용할 수 있습니다. 그런 다음 HttpPost 필터 속성을 내 작업에 추가하여이를 처리 할 수 ​​있습니다.

이제 폼에 검색을 추가해야합니다. 이 작업을 수행하는 가장 쉬운 방법은 검색 양식을 기존 양식 외부에 배치하고 가져올 방법을 설정하는 것입니다. 이 작동하지만 검색 양식을 내 눈금 형태 내에 위치해야 할 경우가 있습니다. 중첩 된 양식을 가질 수 없다는 것을 이해합니다. 따라서 내부 양식의 양식 태그를 제거했지만 이제는 검색을 수행 할 때 게시 요청을 트리거합니다. 여전히 따라 다니는 경우 편집/삭제 작업 메서드가 실행되지만 실제로는 초기 동작을 트리거하고 추가 검색 매개 변수는 필요하다는 것을 알 수 있습니다.

public ActionResult Index(string search) 
{ 
    return GetData(search); 
} 

[HttpPost] 
public ActionResult Index(string command, int id) 
{ 
    switch (command) 
    { 
     case "Delete": 
      DeleteData(id); 
      break; 
     case "Edit": 
      return RedirectToAction("Edit", new { id = id }); 
    } 

    return RedirectToAction("Index"); 
} 

이상적으로 내가 말할 수 있었으면한다 :

여기 내 행동 방법과 같이 무엇을 내가 명령에 따라 호출되는 작업을 필터링하는 방법

public ActionResult Index(string search) 
{ 
    return GetData(search); 
} 

[HttpPost] 
[Command(Name="Delete,Edit")] OR [Command(NameIsNot="Search")] 
public ActionResult Index(string command, int id) 
{ 
    switch (command) 
    { 
     case "Delete": 
      DeleteData(id); 
      break; 
     case "Edit": 
      return RedirectToAction("Edit", new { id = id }); 
    } 

    return RedirectToAction("Index"); 
} 

알 수 있습니다. 어쩌면 나는 여기에 완전히 혼란 스러울 지 모르지만, MVC는 나에게 아주 새로운 것이며, 누군가가 도울 수 있다면 정말 감사 할 것입니다.

감사합니다.

답변

0

경로 제약 조건을 사용하여 수행 할 수 있습니다.

public class CommandConstraint : IRouteConstraint 
{ 
    #region Fields 
    private string[] Matches; 
    #endregion 

    #region Constructor 
    /// <summary> 
    /// Initialises a new instance of <see cref="CommandConstraint" /> 
    /// </summary> 
    /// <param name="matches">The array of commands to match.</param> 
    public CommandConstraint(params string[] matches) 
    { 
     Matches = matches; 
    } 
    #endregion 

    #region Methods 
    /// <summary> 
    /// Determines if this constraint is matched. 
    /// </summary> 
    /// <param name="context">The current context.</param> 
    /// <param name="route">The route to test.</param> 
    /// <param name="name">The name of the parameter.</param> 
    /// <param name="values">The route values.</param> 
    /// <param name="direction">The route direction.</param> 
    /// <returns>True if this constraint is matched, otherwise false.</returns> 
    public bool Match(HttpContextBase context, Route route, 
     string name, RouteValueDictionary values, RouteDirection direction) 
    { 
     if (Matches == null) 
      return false; 

     string value = values[name].ToString(); 
     foreach (string match in Matches) 
     { 
      if (string.Equals(match, value, StringComparison.InvariantCultureIgnoreCase)) 
       return true; 
     } 

     return false; 
    } 
    #endregion 
} 

을 그리고 다음과 같은 내 경로를 프로그램 : 예, 나는 이런 식으로 뭔가를 할 수

분명히
routes.MapRoute("Search", "Home/{command}", 
       new 
       { 
        controller = "Home", 
        action = "Index", 
        command = UrlParameter.Optional 
       }, 
       new { command = new CommandConstraint("Search") }); 

routes.MapRoute("Others", "Home/{command}/{id}", 
       new 
       { 
        controller = "Home", 
        action = "Index", 
        command = UrlParameter.Optional, 
        id = UrlParameter.Optional 
       }, 
       new { command = new CommandConstraint("Delete", "Edit") }); 

당신이 (...) 당신의 색인을 변경해야 할 것 행동 매개 변수 이름 때문에 둘 다 "명령"이지만, 적어도 올바른 방향으로 당신을 도울 수 있습니까?

+0

환호성, 이것은 분명히 올바른 방향으로 나를 인도해야합니다. 다시 한번 감사드립니다. – nfplee

관련 문제