[RESOLVED]Redirecting to different controller on button click

I am working on a very simple CRUD gui and I’m struggling with getting the redirect correct so that it will forward the user to another controller.  Currently it just does nothing.  Below is what i have.

Trying to redirect "Notes" to the NotesModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using Robinson_TestApp.Models; 

namespace Robinson_TestApp.Controllers
{
    public class ProductController : Controller
    {
        private DataTransportDataContext context;
        public ProductController()
        {
            context = new DataTransportDataContext();
        }

        public ActionResult Index()
        {
            IList<ProductModel> ProductList = new List<ProductModel>();
            var query = from Product in context.Products
                        select Product;

            var products = query.ToList();
            foreach (var ProductData in products)
            {
                ProductList.Add(new ProductModel()
                {
                    ID = ProductData.ID,
                    Name = ProductData.Name,
                });
            }
            return View(ProductList);
        }

        public ActionResult Create()
        {
            ProductModel model = new ProductModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(ProductModel model)
        {
            try
            {
                Product prod = new Product()
                {
                    Name = model.Name,
                };
                context.Products.InsertOnSubmit(prod);
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }

        public ActionResult Notes()
        {
            NotesModel model = new NotesModel();
            return RedirectToAction("Index", "NotesController");
        }

        [HttpPost]
        public ActionResult Notes(NotesModel model)
        {
            return RedirectToAction("Index", "NotesController");
        }

        protected override void Dispose(bool disposing)
        {
            context.Dispose();
            base.Dispose(disposing);
        }
    }
}

Here is the NotesController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Robinson_TestApp.Models;

namespace Robinson_TestApp.Controllers
{
    public class NotesController : Controller
    {
        private DataTransportDataContext context;
        
        public NotesController()
        {
            context = new DataTransportDataContext();
        }

        private void CreateNotes(NotesModel model)
        {
            model.Notes = context.Products.AsQueryable<Product>().Select(x => 
                new SelectListItem()
            {
                Text = x.Name,
                Value = x.ID.ToString()
            });
        }

        public ActionResult Index()
        {
            IList<NotesModel> NoteList = new List<NotesModel>();
            var query = from note in context.ProductNotes
                        join product in context.ProductNotes
                        on note.ID equals product.ID
                        select new NotesModel
                        {
                            ID = note.ID,NoteText = note.NoteText, Date = note.CreateDate //Archived = note.Archived
                        };
            NoteList = query.ToList();
            return View(NoteList);
        }

        public ActionResult Details(int ID)
        {
            NotesModel model = context.ProductNotes.Where(x => x.ID == ID).Select(x =>
                                                    new NotesModel()
                                                    {
                                                        ID = x.ID,
                                                        NoteText = x.NoteText,
                                                        Date = x.CreateDate,
                                                        //Archived = x.Archived
                                                    }).SingleOrDefault();
            return View(model);
        }

        public ActionResult Create()
        {
            NotesModel notes = new NotesModel();
            CreateNotes(notes);
            return View(notes);
        }

        [HttpPost]
        public ActionResult Create(NotesModel model)
        {
            try
            {
                ProductModel prod = new ProductModel();
                DateTime date = new DateTime();
                ProductNote note = new ProductNote()                
                {
                    ID = prod.ID,
                    NoteText = model.NoteText,
                    CreateDate = date.Date
                };
                context.ProductNotes.InsertOnSubmit(note);
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }
        public ActionResult Edit(int id)
        {
            NotesModel notes = context.ProductNotes.Where(x => x.ID == id).Select(x =>
                                 new NotesModel()
                                 {
                                     ID = x.ID,
                                     NoteText = x.NoteText,
                                     Date = x.CreateDate,
                                 }).SingleOrDefault();

            CreateNotes(notes);
            return View(notes);
        }

        [HttpPost]
        public ActionResult Edit(NotesModel model)
        {
            try
            {
                ProductNote note = context.ProductNotes.Where(x => x.ID == model.ID).Single<ProductNote>();
                note.NoteText = model.NoteText;
                note.CreateDate = model.Date;
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }

        public ActionResult Delete(int id)
        {
            NotesModel model = context.ProductNotes.Where(x => x.ID == id).Select(x =>
                                   new NotesModel()
                                   {
                                       ID = x.ID,
                                       NoteText = x.NoteText,
                                       Date = x.CreateDate,
                                   }).SingleOrDefault();
            return View(model);
        }
        
        [HttpPost]
        public ActionResult Delete(NotesModel model)
        {
            try
            {
                ProductNote note = context.ProductNotes.Where(x => x.ID == model.ID).Single<ProductNote>();
                note.NoteText = model.NoteText;
                note.CreateDate = model.Date;
                context.SubmitChanges();
                note.Archived = true;
                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }
    }
}

 

The easiest way is to use TempData to pass model to your Notes controller

        [HttpPost]
        public ActionResult Notes(NotesModel model)
        {
            TempData["Model"] = model;
            return RedirectToAction("Index", "Notes");
        }

Do I need to declare that TempDataDictionary in the Product Model?

public TempDataDictionary TempData { get; set; }

Hi,

No you don’t have to declare it in your model. The tempdata dictionary is just available , but its values only till the next request. Pls see:
http://msdn.microsoft.com/en-us/library/dd394711(v=vs.100).Aspx for further details.

Regards,

Yorrick

Thank you Yorrick, it doesnt appear this is the way I should be doing this.  I need this to work everytime, and currently as I have it written it isnt working at all.  It appears to be redirecting to the index of the controller its currently using vs to
the other controller….  Here is the product controller where I added the TempData function.  I’ve read the msdn link you posted and I’m not sure where I’ve gone wrong!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using Robinson_TestApp.Models;
using Robinson_TestApp.Controllers; 

namespace Robinson_TestApp.Controllers
{
    public class ProductController : Controller
    {
        private DataTransportDataContext context;
        public ProductController()
        {
            context = new DataTransportDataContext();
        }

        public ActionResult Index()
        {
            IList<ProductModel> ProductList = new List<ProductModel>();
            var query = from Product in context.Products
                        select Product;

            var products = query.ToList();
            foreach (var ProductData in products)
            {
                ProductList.Add(new ProductModel()
                {
                    ID = ProductData.ID,
                    Name = ProductData.Name,
                });
            }
            return View(ProductList);
        }

        public ActionResult Create()
        {
            ProductModel model = new ProductModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(ProductModel model)
        {
            try
            {
                Product prod = new Product()
                {
                    Name = model.Name,
                };
                context.Products.InsertOnSubmit(prod);
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }

        public ActionResult Notes()
        {
            NotesModel model = new NotesModel();
            return RedirectToAction("Index", "NotesController");
        }

        [HttpPost]
        public ActionResult Notes(NotesModel model)
        {
            TempData["Notes"] = model;
            return RedirectToAction("Index", "Notes");
        }

        protected override void Dispose(bool disposing)
        {
            context.Dispose();
            base.Dispose(disposing);
        }
    }
}

Hi th3knick,

Thanks for your post.

th3knick

Do I need to declare that TempDataDictionary in the Product Model?

You didn’t need to declare TempDataDictionary,it’s a property under ControllerBase Class.

Please check this:http://msdn.microsoft.com/en-us/library/system.web.mvc.controllerbase(v=vs.118).aspx

More information about TempData,you can check this:

#What is ViewData, ViewBag and TempData? – MVC options for passing data between current and subsequent request

http://www.codeproject.com/Articles/476967/WhatplusisplusViewData-cplusViewBagplusandplusTem

Hope this can be helpful.

Best Regards,

Eileen

Hi,

Why it isn’t redirecting to your "Notes" Controller, assuming that the controller exist and the name is spelled correctly, I can’t say. Your code sample should work. But What I don’t understand in your code below is why this solution:

        [HttpPost]
        public ActionResult Notes(NotesModel model)
        {
            TempData["Notes"] = model;
            return RedirectToAction("Index", "Notes");
        }

It seems that you post to this method from your view and after that use "TempData" and "RedirectToAction" to transport the model to the "Index" method of the "Notes" Controller. This doesn’t seem to be very economical. Why do you do this this way? Why not
posting directly to the “Notes” controller from the view? That way you only have to do one request instead of two.

Another thing that could be refactored into something more economical ;-) is the following:

public ActionResult Index()
        {
            IList<ProductModel> ProductList = new List<ProductModel>();
            var query = from Product in context.Products
                        select Product;

            var products = query.ToList();
            foreach (var ProductData in products)
            {
                ProductList.Add(new ProductModel()
                {
                    ID = ProductData.ID,
                    Name = ProductData.Name,
                });
            }
            return View(ProductList);
        }

into:

public ActionResult Index()
{
	var productList = (from product in context.Products 
			   select new ProductModel
			   {
				ID = product.ID,
			        Name = product.Name										
			    }).ToList();
	return View(productList);
}

Hope this helps.

Regards,

Yorrick

Leave a Reply