0

I have a SubmitComment Action and a Message class. I want to use Message class for sending that the action is done successfully or not, but Message.Result is false and Message.Text is null when I check them in ShowProduct action.

public class Message
{
    public bool Result { get; set; }
    public string Text { get; set; }
}

My SubmitComment Action:

public ActionResult SubmitComment(int productId, string comment, string nickName)
{
        try
        {
            var productCM = new ProductCM
            {
                //I make my prodcut comment object here
            };
            storesDB.ProductCMs.Add(productCM);
            storesDB.SaveChanges();

            Message message = new Message
            {
                Result = true,
                Text = "Your comment successfully registered."
            };

            return RedirectToAction("ShowProduct", new { id = productId, message });
        }
        catch (//if any exeption happend)
        {
            Message message = new Message
            {
                Result = false,
                Text = "Sorry your comment is not registered."
            };

            return RedirectToAction("ShowProduct", new { id = productId, message });
        }
}

My ShowProduct Action:

public ActionResult ShowProduct(int? id, message)
{
    ViewBag.Message = message;

    var model = storesDB.Products;

    return View(model);
}

What's the problem?

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114

2 Answers2

1

You cannot pass object with RedirectToAction. Alternatively, instead of return RedirectToAction("ShowProduct", new { id = productId, message }) use return ShowProduct(productId,message); as follows:

Message message = new Message
{
    Result = true,
    Text = "Your comment successfully registered."
};

return ShowProduct(productId,message);
TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
  • It works but I want to show ShowProduct view after that I submited the comment, but It want to show SubmitComment View!!! Can you help about that? – Farshad Shahsavari Feb 13 '19 at 12:56
  • @FarshadShahsavari Mark it answer and better ask a different question with the new requirement clearly. I shall try level best to help you. – TanvirArjel Feb 13 '19 at 12:58
0

For RedirectToAction, it will pass the parameter as query string, and you could not pass the embeded object.

Try to specify the properties like

return RedirectToAction("ShowProduct", new { id = productId, message.Result, message.Text });

For return ShowProduct(productId,message);, specify the ViewName like

public ActionResult ShowProduct(int? id, Message message)
{
    ViewBag.Message = message;

    return View("ShowProduct");
}
Edward
  • 28,296
  • 11
  • 76
  • 121