2011-03-31 6 views
0

간단한 폼, 텍스트 상자 및 명령 단추가 있습니다. 나는 방법 = "게시물"컨트롤러에 텍스트 상자에 입력 된 값을 얻으려면 사용하고 있습니다. 컨트롤러 방법은 다음과 같습니다ASP.net MVC 컨트롤러 FormCollection 및 정수 가져 오기

public ActionResult Index(FormCollection form) 
     { 

이 모든 좋은,하지만 나중에 나는 다음과 같은 사 용할 수있는 라인 아래로 : 리턴 RedirectToAction ("../ MyFolder에/MYVIEW /"+ MYID을); 그러나 초기보기 (색인)가 양식 모음을 전달하여 작업했기 때문에 위의 작업을 수행 할 수 없습니다. 어떻게하면 가능합니까? 어떤 도움이라도 대단히 감사하겠습니다!

내가 거의 질문에서 어떤 의미를 만들 수 있습니다

답변

2

그러나 여기 당신이 도움이 될 수 있습니다 ASP.NET MVC에서 일반적으로 사용되는 패턴입니다 : 내가 질문을 받았다면

// Used to render some form allowing to edit a model 
public ActionResult Index(int id) 
{ 
    var model = _someRepository.Get(id); 
    return View(model); 
} 

// used to handle the submission of the form 
[HttpPost] 
public ActionResult Index(SomeViewModel model) 
{ 
    if (!ModelState.IsValid) 
    { 
     // the model was invalid => redisplay the form insulting the user 
     return View(model); 
    } 
    // TODO: the user entered valid information => process the model 
    // and redirect 
    return RedirectToAction("Index", new { id = model.Id }); 
} 
+0

나는 다음과 같은 코드를했는데, 나는 방법에 리디렉션하려고이 코드가 있습니다. return RedirectToAction ("../ MyFolder/Index /"+ ID); 여기에 중단 점을 배치 할 때 ID 내부에 값이 있습니다. 실행될 때 공용 ActionResult 인덱스 (int id) {하지만 ID가 null이라는 것을 알 수 있습니다. 왜 그런가? –

+0

@Cal, 내 대답에'RedirectToAction' 메서드를 사용하고있는 것을 보았습니까? RedirectToAction ("../ MyFolder/Index"+ ID}) "를 반환하지 말고'RedirectToAction ("Index ", new {id = ID} 또한 현재 컨트롤러와 다른 경우 컨트롤러 이름을 사용하는 오버로드가 있습니다. –

1

, 당신은 귀하의 방법을 다시 작성할 수는 당신이 사용할 수있는보다

public ActionResult Index(int id, FormCollection forms) 
    {...} 

모두 formsid