2013-08-05 2 views
0

내보기에 아약스 양식이 있습니다양식 제출 단추 대신 아약스 링크를 사용하는 방법?

@using (Ajax.BeginForm("SearchHuman", "Search", new AjaxOptions(){ 
InsertionMode = InsertionMode.Replace, 
UpdateTargetId = "result" })) 

만을 사용하여 {

<div class="editor-field"> 
@DescriptionStrings.Lastname: 
@Html.TextBox("LastName") 
</div> 

<div class="editor-field"> 
@DescriptionStrings.Firstname: 
@Html.TextBox("Name") 
</div> 

//submit button 
<input type="submit" value='Start Searching' /> 

//submit link 
@Ajax.ActionLink("search", "OtherSearch", new{lastName ="",...}, new AjaxOptions() 
     { 
      InsertionMode = InsertionMode.Replace, 
      UpdateTargetId = "tab" 
     }) 

} 내가 버튼 (다른 데이터베이스에서) 2 개 개의 다른 검색에 대한 링크를 제출해야 할

하나의 양식. 그러나 양식의 텍스트 상자에서 경로 값을 Ajax.ActionLink로 전달하는 방법은 무엇입니까? 사전에

감사합니다!

답변

0

는 우리가 선택한 해결책은 우리가 기반으로 눌러 진 버튼을 차별화 할 수 사용자 정의 ActionMethodSelectorAttribute을 구현하기위한 것이었다는 이름 속성 그런 다음 ActionName 데코레이터에서 BeginFrom 도우미에 지정된 것과 동일한 액션 이름을 지정하는 여러 메서드를 꾸며서 사용자 지정 ActionMethodSelector 데코레이터를 사용하여 클릭 한 버튼의 이름을 기반으로 호출 할 메서드를 구별했습니다 . 결과적으로 각 제출 단추는 호출되는 별도의 메소드로 연결됩니다.

일부 코드를 설명하기 : 컨트롤러에서

:

보기에서
[ActionName("RequestSubmit")] 
[MyctionSelector(name = "Btn_First")] 
public ActionResult FirstMethod(MyModel modelToAdd) 
{ 
    //Do whatever FirstMethod is supposed to do here 
} 

[ActionName("RequestSubmit")] 
[MyctionSelector(name = "Btn_Second")] 
public ActionResult SecondMethod(MyModel modelToAdd) 
{ 
    //Do whatever SecondMethod is supposed to do here 
} 

:

public string name { get; set; } 
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) 
{ 
    var btnName = controllerContext.Controller.ValueProvider.GetValue(name); 
    return btnName != null; 
} 
+0

것은 당신이 어떤 조언을 주 시겠어요 : 사용자 지정 특성에 관해서는

@using (Ajax.BeginForm("RequestSubmit",..... <input type="submit" id="Btn_First" name="Btn_First" value="First"/> <input type="submit" id="Btn_Second" name="Btn_Second" value="Second"/> 

속성은 내 사용자 정의 속성을 확장해야합니까? NonActionAttribute입니까? –

+1

확장해야합니다 : ActionMethodSelectorAttribute. – JTMon

1

그러나 경로 값을 양식의 텍스트 상자에서 Ajax.ActionLink로 전달하는 방법은 무엇입니까?

수 없습니다. 값을 서버로 보내려면 제출 단추를 사용해야합니다. 두 컨트롤러 모두 동일한 컨트롤러 작업에 제출하는 동일한 양식의 제출 버튼 2 개를 가질 수 있습니다. 그런 다음이 작업 내에서 클릭 한 버튼을 테스트하고 값에 따라 하나 또는 다른 검색을 수행 할 수 있습니다.

예 :

<button type="submit" name="btn" value="search1">Start Searching</button> 
<button type="submit" name="btn" value="search2">Some other search</button> 

다음 컨트롤러 액션 내부 :

[HttpPost] 
public ActionResult SomeAction(string btn, MyViewModel model) 
{ 
    if (btn == "search1") 
    { 
     // the first search button was clicked 
    } 
    else if (btn == "search2") 
    { 
     // the second search button was clicked 
    } 

    ... 
} 
관련 문제