How to display a message using ViewBag after form submit

SPIDEY JOE 21 Reputation points
2022-05-27T13:19:11.353+00:00

[HttpPost]
public ActionResult AddCat(CategoryModel obj)
{
try
{

            using (SqlConnection Sqlconn = new SqlConnection(connectionString))
            {
                Sqlconn.Open();
                string query1 = "SELECT CatName FROM CategoryDetail WHERE CatName=@catname";
                SqlCommand cmdd = new SqlCommand(query1, Sqlconn);
                cmdd.Parameters.AddWithValue("@catname", obj.obj1.CategoryName);
                SqlDataAdapter chdb = new SqlDataAdapter(cmdd);
                DataTable CheckDbTbl = new DataTable();
                chdb.Fill(CheckDbTbl);
                Sqlconn.Close();
                if (CheckDbTbl.Rows.Count > 0)
                {
                    ViewBag.Message = "Inserted";
                    return RedirectToAction("SampleGrid");
                }
                else
                {
                    Sqlconn.Open();
                    string query = "INSERT INTO CategoryDetail VALUES (@catname,@catstatus)";
                    SqlCommand qlcmd = new SqlCommand(query, Sqlconn);
                    qlcmd.Parameters.AddWithValue("@catname", obj.obj1.CategoryName);
                    qlcmd.Parameters.AddWithValue("@catstatus", obj.obj1.CategoryStatus);
                    qlcmd.ExecuteNonQuery();
                    string Inserted = "Record Inserted successfully";
                    ViewBag.Message = Inserted;
                }
            }

            return RedirectToAction("SampleGrid");
        }

        catch (Exception ex)
        {
            return Content(ex.Message.ToString());
        }
    }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,255 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,066 Reputation points
    2022-05-30T03:29:35.543+00:00

    Hi @SPIDEY JOE ,
    Why you redirect to the "SamlpeGrid"? I think you need return the view. And then in the page, you need to show it. Just like this:

    @using (Html.BeginForm("action", "controller", FormMethod.Post))  
    {  
        @Html.EditorFor(m => m.UserName)  
      
       <input type="submit" value="Submit" />  
    }  
      
    @{  
      if(ViewBag.Message != null)  
      {  
         <div>  
             @ViewBag.Message  
         </div>  
      }  
    }  
    
    [HttpPost]  
    public ActionResult AddCat(CategoryModel obj)  
    {  
        .......  
        return view();  
    }  
    

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 26,201 Reputation points
    2022-05-27T14:22:40.877+00:00

    The ViewBag passes data to a view and is only good for the current request. Your code does not return a View it returns a redirect which causes the browser to do an HTTP request to SampleGrid.

    Use TempData to persist data between requests.

    https://www.tutorialsteacher.com/mvc/tempdata-in-asp.net-mvc

    0 comments No comments