2013-05-22 3 views
1

저는 ASP.net MVC를 처음 사용하며 현재이 작업을 수행하기 위해 애 쓰고 있습니다.양식을 제출 한 후 컨트롤러가 리디렉션하지 않습니다.

데이터는 방법에 POST를 통해 전송되면
public ActionResult Add() 
{ 
    // check user is authenticated 
    if (Request.IsAuthenticated) 
    { 
     return View(); 
    } 

    return RedirectToAction("Index", "Home"); 
} 

// 
// POST: /Home/Add 

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Add(string title, string description, string priority, string color, FormCollection collection) 
{ 
    if (ModelState.IsValid) 
    { 
     // create instance of todo object 
     todo obj = new todo(); 

     try 
     { 
      // gather fields 
      obj.priority = Convert.ToInt32(priority); 
      obj.color = Convert.ToInt32(color); 
      obj.title = title; 
      obj.description = description; 

      todosDataContext objLinq = new todosDataContext(); 

      // get the users id, convert to string and store it 
      var userid = Membership.GetUser().ProviderUserKey; 
      obj.userid = userid.ToString(); 

      // save 
      objLinq.todos.InsertOnSubmit(obj); 
      objLinq.SubmitChanges(); 

      return RedirectToAction("Index", "Home"); 
     } 
     catch 
     { 
      return View(obj); 
     } 
    } 

    return RedirectToAction("Index", "Home"); 
} 

은, 데이터베이스에 데이터를 추가해야합니다 : 나는이처럼 보이는 컨트롤러 방법은 Add라고합니다. 그게 잘 작동하고 모든 것이 올바르게 추가됩니다. 그러나 RedirectToAction은 실행 중이 아니며 응용 프로그램이 /Home/Index으로 리디렉션되어야 할 때 /Home/Add에서 중단됩니다. 보기가로드되므로 /Home/Index이 표시되지만 URL에 /Home/Add이 표시됩니다. 그러나 리디렉션 고장,

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<todo_moble_oauth.Models.todo>" %> 

<% using (Html.BeginForm()) { %> 
<%: Html.AntiForgeryToken() %> 
<%: Html.ValidationSummary(true) %> 

<fieldset> 

    <h3>Title:</h3> 
    <div class="editor-field"> 
     <input type="text" name="title" /> 
    </div> 

    <h3>Description:</h3> 
    <div class="editor-field"> 
     <input type="text" name="description" /> 
    </div> 

    <h3>Priority:</h3> 
    <div class="editor-field"> 
     <select name="priority"> 
      <option value="1">Low</option> 
      <option value="2">Medium</option> 
      <option value="3">High</option> 
     </select> 
    </div> 

    <div data-role="fieldcontain"> 
     <fieldset data-role="controlgroup"> 
      <h3>Color:</h3> 
      <input type="radio" name="color" id="radio-choice-1" value="0" checked="checked" /> 
      <label for="radio-choice-1">None</label> 

      <input type="radio" name="color" id="radio-choice-2" value="1" /> 
      <label for="radio-choice-2">Red</label> 

      <input type="radio" name="color" id="radio-choice-3" value="2" /> 
      <label for="radio-choice-3">Blue</label> 

      <input type="radio" name="color" id="radio-choice-4" value="3" /> 
      <label for="radio-choice-4">Yellow</label> 
     </fieldset> 
    </div> 

    <p> 
     <input type="submit" value="Create" /> 
    </p> 

</fieldset> 
<% } %> 

그래서 데이터가 데이터베이스로 전송되어 저장되고 : 여기

폼을 포함하는 부분적인 화면의 사본이다.

+1

그것은 다음 예외가 발생합니다. catch 블록 안에 중단 점을 넣으십시오. –

+0

예, 예외가있어서 catch 블록을 실행 중입니다'return View (obj); – Jasen

+0

잘 실행 중이고 데이터가 저장되고 리다이렉션이 수행 중일 때 주소 URL이 바뀌지 않습니다 그러나 응용 프로그램은 Index() 뷰를로드합니다. – Neil

답변

관련 문제