2014-01-31 31 views
0

您好,因爲我對Mvc是如此的新穎我感謝任何幫助!我的動作不會將我重定向到索引視圖

因此,這裏是myModels

類別

-CategoryID

-DateCreated

CategoriesLanguages

ID(自動增量)

類別ID

LanguageID

標題

說明

基本上當我在我的索引單擊添加按鈕()查看 - 我重定向到AddCategory()動作,其剛加入新記錄在類別表中,並返回一個文本框和按鈕,用戶可以在其中填充CategoriesLanguages表的數據視圖 當我點擊按鈕時,我做了一個ajax請求AddCategoriesLanguages()的行動,一切都很好 - 它添加是數據庫中的記錄,但最後我說RedirectToAction(「索引」)沒有任何反應。

這裏是我的CategorViewModel.cs

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); 

      } 

這裏是我的CategoryController

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 sucess頁面 – 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致電AddCategoriesLanguages行動。從該動作返回HTTP 301不會在瀏覽器中重新加載頁面,因爲它是AJAX調用。

你可以從AddCategoriesLanguages行動的URL,並在成功回調在$就調用返回JSON做

windows.location = result.url;

RedirectToAction只有在您完成整頁POST時纔有效。您可以用表單發佈替代Ajax調用,但這顯然會對您的應用的客戶端體系結構產生重大影響。

相關問題