question

SPIDEYJOE-3858 avatar image
0 Votes"
SPIDEYJOE-3858 asked YijingSun-MSFT commented

How to display a message using ViewBag after form submit

[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());
         }
     }
dotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YijingSun-MSFT avatar image
1 Vote"
YijingSun-MSFT answered YijingSun-MSFT commented

Hi @SPIDEYJOE-3858 ,
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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I don't have a view for AddCat, the form is in SampleGrid View so I redirected the action to SampleGrid

0 Votes 0 ·

Hi @SPIDEYJOE-3858 ,
Your action is AddCat, you only need to return view.

Best regards,
Yijing Sun

1 Vote 1 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.