Reputation: 1908
Hello as I'm so new to Mvc I woild appreciate any Help!
So Here is myModels
Category
-CategoryID
-DateCreated
CategoriesLanguages
ID (autoincrement)
CategoryID
LanguageID
Title
Description
Basically when I click Add button in my Index() View - I'm redirected to AddCategory() Action which just add a new record in Categories Table and returns me a view with text boxes and button in which the user can populate the data for CategoriesLanguages table When i click the button I make an ajax request to AddCategoriesLanguages()action and everything is fine - it adds the record in the database but at the end when i say RedirectToAction("Index") nothing happens.
here is my 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);
}
Here is my 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");
}
Here is my 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=' [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" />
Upvotes: 0
Views: 65
Reputation: 6398
Try like this in your view
$.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.
},
Upvotes: 1
Reputation: 46008
You are doing an AJAX call to the AddCategoriesLanguages
action. Returning HTTP 301 from that action won't reload the page in the browser as it's an AJAX call.
You could return Json from AddCategoriesLanguages
action with the url and in the success callback in your $.ajax call do
windows.location = result.url;
RedirectToAction
works only if you're doing a full page POST. You might replace Ajax call with form post, but it's obviously a big impact on your app's client-side architecture.
Upvotes: 0