您好,因爲我對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" />
,你可以嘗試重新加載ajax.call sucess頁面 – Miller