我的动作不会将我重定向到索引视图

时间:2014-01-31 12:17:02

标签: asp.net-mvc

您好,我是Mvc的新手,我很高兴感谢任何帮助!

所以这是myModels

分类

-CategoryID

-DateCreated

CategoriesLanguages

ID(自动增量)

类别ID

LanguageID

标题

描述

基本上当我点击我的索引()视图中的添加按钮时 - 我被重定向到AddCategory()Action,它只是在Categories Table中添加一条新记录,并返回一个带有文本框和按钮的视图,用户可以在其中填充CategoriesLanguages表的数据 当我单击按钮时,我向AddCategoriesLanguages()动作发出ajax请求,一切都很好 - 它在数据库中添加记录,但最后我说RedirectToAction(“Index”)没有任何反应。

这是我的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");

        }

这是我的View 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=' +@Model.newCategoryID +'&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" />

2 个答案:

答案 0 :(得分:1)

在您的视图中尝试这样

    $.ajax({

        type: "GET",
        url: '@Url.Action("AddCategoriesLanguages")' + '?catID=' +@Model.newCategoryID +'&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.
}, 

答案 1 :(得分:0)

您正在对AddCategoriesLanguages操作进行AJAX调用。从该操作返回HTTP 301将不会在浏览器中重新加载页面,因为它是一个AJAX调用。

您可以使用网址从AddCategoriesLanguages操作返回Json,并在$ .ajax调用中成功回调

  

windows.location = result.url;

RedirectToAction仅在您执行完整页面POST时才有效。您可以使用表单发布替换Ajax调用,但这显然会对您的应用程序的客户端体系结构产生重大影响。