2014-09-08 1 views
0

내보기 2 레이블 2 텍스트 상자 및 단추가 있습니다. 나는이 프로그램을 실행하면Http 게시물 처리 및 동일한보기에서 가져 오기 - ASP.Net MVC 면도날

public ActionResult Create() 
{ 
    // Custom model that holds values 
    // this is to set the default values to the text boxes 

    return View(model); 
} 

[HttpPost] 
public ActionResult Create(CustomModel viewModel) 
{ 
    try 
    { 
     // TODO: Add insert logic here 

     // The button should trigger this method to perform update 

     return RedirectToAction("Create"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 

자동으로 값을 포함하지 널 포인터 예외가 발생 내 모델로 이동 :

이 내 컨트롤러입니다. 모델 상태를 보존하는 방법에 대한 아이디어. ?

업데이트 : 나는 고전적인 ADO.Net 모델되지에게 엔티티 프레임 워크를 사용하고

.

http getpost method은 다른 논리를 따릅니다. 값이있는 모델은 get 메소드를 사용하여 리턴되며 post 메소드를 사용하여 해당 레코드를 데이터베이스로 갱신하기 위해이 모델의 상태를 보존해야합니다.

그러나 post 메서드를 사용할 때 컴파일러는 매개 변수가없는 생성자를 찾는 내 모델 클래스로 자동으로 라우팅됩니다. 내 모델 클래스의 새로운 인스턴스를 만드는 것 같습니다.

+0

더 구체적이고 자세한 정보를 제공 할 수 있습니까? – Jonesopolis

+0

보기에 대한 코드를 포함 할 수 있습니까? –

+0

@ 존경 추가 정보를 추가했습니다. – Vikram

답변

0

그럼 당신이보기에 검증을 넣고 아래 코드와 같이 다음 ModelState.IsValid 속성을 사용할 수 있습니다 : -

[HttpPost] 
    public ActionResult Create(CustomModel viewModel) 
    { 
     if (ModelState.IsValid) 
       { 
        ////Insert logic here 
        return RedirectToAction("Create"); 
       } 

     return View(viewModel); 
    } 
0

없음 완전히 확인 내가 따라,하지만 당신은 전달하여 POST 액션에서 동일한 뷰를 반환 할 수 있습니다 모델에서. 그러면 모델 데이터가 보존됩니다.

[HttpPost] 
public ActionResult Create(CustomModel viewModel) 
{ 
    try 
    { 
     // TODO: Add insert logic here 

     // The button should trigger this method to perform update 

     // Return "Create" view, with the posted model 
     return View(model); 
    } 
    catch 
    { 
     // Do something useful to handle exceptions here. 

     // Maybe add meaningful message to ViewData to inform user insert has failed? 
     return View(model); 
    } 
} 
0

"Get"메서드는 코드에 존재하지 않는 모델 (여기에 표시된 모델)을 반환합니다. 아래는 귀하의 행동이 GET & POST를 받아 들일 수있게하는 방법입니다.

[AcceptVerbs(HttpVerbs.Get|HttpVerbs.Post)] 
public ActionResult Create(CustomModel viewModel) 
{ 
    try 
    { 
     // TODO: Add insert logic here 

     // The button should trigger this method to perform update 

     return RedirectToAction("Create"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 
0

당신은 당신의 Action Result 방법 create new instance에 있습니다.

public ActionResult Create() 
{ 
    // Assuming - First Time this ActioResult will be called. 
    // After your other operations 

    CustomModel model = new CustomModel(); 

    // Fill if any Data is needed 
    return View(model); 

    // OR - return new instance model here 
    return View(new CustomModel()); 
} 
관련 문제