2014-01-31 2 views
0

안녕하세요. 저는 Mvc에 너무 익숙하지 않았기 때문에 어떤 도움을 주셔서 감사합니다! 그래서 여기내 작업이 나를 인덱스보기로 리디렉션하지 않습니다.

는 myModels

-CategoryID

CategoriesLanguages에게

ID (자동 증가)

,536을 -DateCreated 종류 인

카테고리 ID

내가) (내 색인에서보기를 추가 버튼을 클릭하면

설명

기본적으로

제목의 LanguageID - 나는 AddCategory에 단지 새로운 추가() 액션을 리디렉션 해요 Categories 테이블에 레코드하고 나에게 사용자가 CategoriesLanguages ​​테이블 에 대한 데이터를 채울 수있는 텍스트 상자와 버튼이있는 뷰를 반환합니다. 버튼을 클릭하면 AddCategoriesLanguages ​​() 액션에 대한 AJAX 요청을 만들고 모든 것이 괜찮습니다. 데이터베이스의 레코드지만 결국 내가 말할 때 RedirectToAction ("인덱스") 아무것도 일어나지 않습니다. 여기

여기
public class CategoryViewModel 
     { 

      public List<Language> lstLanguages { get; set; } 
      public List<CategoryLanguages> lstCategoryLanguages { get; set; } 
      public CategoryLanguages categoryToEdit { get; set; } 
      private readonly ICategoryRepository catRep; 
      private readonly ILanguageRepository lanRep; 
      private readonly ICategoryLanguageRepository catlanRep; 


      public CategoryViewModel() 
       : this(new CategoryRepository(),new LanguageRepository(),new CategoryLanguageRepository()) 
      { 

      } 

      public CategoryViewModel(ICategoryRepository catRep, ILanguageRepository lanRep, ICategoryLanguageRepository catlanRep) 
      { 
       this.catRep = catRep; 
       this.lanRep = lanRep; 
       this.catlanRep = catlanRep; 
      } 


      public void AddNewCategory() 
      { 
       lstLanguages = lanRep.GetAllAvailableLanguages(); 
       newCategoryID = catRep.AddCategory(); 

      } 

      public void AddCategoriesLanguages(int catID, int lanID, string title, string shortDescription, string description) 
      { 
       catlanRep.AddCategoryLanguage(catID, lanID, title, shortDescription, description); 

      } 

public class CategoryController : Controller 
    { 
     public ActionResult Index() 
     { 
      CategoryViewModel ob = new CategoryViewModel(); 
      ob.LoadLanguages(); 
      return View(ob); 
     } 



     public ActionResult AddCategory() 
     { 
      CategoryViewModel vm = new CategoryViewModel(); 
      vm.AddNewCategory(); 
      return View(vm); 
     } 

     public ActionResult AddCategoriesLanguages(int catID, int lanID, string title, string shrtDescription, string description) 
     { 
      CategoryViewModel vm = new CategoryViewModel(); 
      vm.AddCategoriesLanguages(catID, lanID, title, shrtDescription, description); 
      return RedirectToAction("Index"); 

     } 
다음

내보기 AddCategory.cshtml

@model Onion.Web.ViewModels.CategoryViewModel 



<script> 
    $(document).ready(function() { 
     $('#btnAdd').click(function() { 
      var variab = 2; 

      $.ajax({ 

       type: "GET", 
       url: '@Url.Action("AddCategoriesLanguages")' + '?catID=' [email protected] +'&lanID=' + $("#ddlLanguages").val() + '&title=' + $('#txbTitle').val() + '&shrtDescription=' + $('#txbShortDescription').val() + '&Description=' + $('#txbDescription').val(), 
       data: {} 




      }); 
     }); 
    }); 
    </script> 

<h2>AddCategory</h2> 
@Html.DropDownList("Languages", new SelectList(Model.lstLanguages, "LanguageID", "Name",@HttpContext.Current.Session["langID"]),new { id = "ddlLanguages" }) 
<br /> 
<label for="txbTitle">Title:</label> 
<input type="text" id="txbTitle"/> 
<br /> 
<label for="txbShortDescription">Short Description:</label> 
<input type="text" id="txbShortDescription" /> 
<br /> 
<label for="txbDescription">Description:</label> 
<input type="text" id="txbDescription" /> 
<br /> 
<br /> 
<input type="button" id="btnAdd" value="Add" /> 
+0

당신이 ajax.call 성공에 페이지를 다시로드 시도 할 수 있습니다보기에이 같은 시도 – Miller

답변

1

$.ajax({ 

     type: "GET", 
     url: '@Url.Action("AddCategoriesLanguages")' + '?catID=' [email protected] +'&lanID=' + $("#ddlLanguages").val() + '&title=' + $('#txbTitle').val() + '&shrtDescription=' + $('#txbShortDescription').val() + '&Description=' + $('#txbDescription').val(), 
     data: {}, 
success: function() 
{ 
     var redirect='YOUR URL'; 
     Window.location=redirect; // dont do anything. --problem. 
}, 
0

당신이 AJAX를하고있는 내 CategoryController 내 CategorViewModel.cs입니다로 전화조치. 해당 작업에서 HTTP 301을 반환해도 AJAX 호출이므로 브라우저에서 페이지를 다시로드하지 않습니다.

당신은 $ 아약스 호출에서 URL을 AddCategoriesLanguages 행동에서와 성공 콜백에서 JSON을 반환 할 수 할

windows.location = result.url;

RedirectToAction은 전체 페이지 POST를 수행하는 경우에만 작동합니다. 양식 게시로 Ajax 호출을 대체 할 수는 있지만 앱의 클라이언트 측 아키텍처에 큰 영향을 미칩니다.

관련 문제