Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends

i can't send data through the viewdata or viewbag from controller to view.
please help me to resolve this.
here is my code:
Controller

public ActionResult BookCourse(string Id)
        {
            services();
            var item = _courseDetailsService.GetAll();
            ViewBag.Code = Id.ToString();
            ViewBag.Course = item;
            TempData["code"] = Id;
            ViewBag.title = "Booking Course";
            ViewBag.Flag = false;
            return View();
        }
        [HttpPost]

        [RecaptchaControlMvc.CaptchaValidator]
        public ActionResult BookCourse(CourseBooking book, bool captchaValid, string captchaErrorMessage)
        {
            try
            {
                services();
                string code = TempData["code"].ToString();
                    bool valid = true;
                
                    if (!captchaValid)
                    {
                        ViewBag.ErrorMessage = "Invalid Entry *";
                        ModelState.AddModelError("Captcha", "Please Enter verification text correctly");
                        valid = false;
                    }

                    if (book.FirstName == null || book.FirstName == "")
                    {
                        ModelState.AddModelError("FirstName", "Enter First Name");
                        valid = false;
                    }
                    if (book.LastName == null || book.LastName == "")
                    {
                        ModelState.AddModelError("LastName", "Enter Last Name");
                        valid = false;
                    }


                    if (book.EmailAddress == null || book.EmailAddress == "")
                    {
                        ModelState.AddModelError("EmailAddress", "Enter Email Address");
                        valid = false;
                    }
                    else
                    {
                        string emailRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                                                 @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                                                    @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
                        Regex re = new Regex(emailRegex);
                        if (!re.IsMatch(book.EmailAddress))
                        {
                            ModelState.AddModelError("Email", "Email Address is not valid");
                            valid = false;
                        }
                    }
                    if (!valid)
                    {
                        ViewBag.Flag = true;                        
                        return BookCourse(code);
                    }
                    book.DateBooked = DateTime.Now;                
                    _courseBookingService.Add(book);
                    SendNotificationMail(book);                   
            }
            catch(Exception ex)
            {
                _log.Log(ex);
            }            
                return RedirectToAction("Thanks");           
        }
View(Book Course)
 @if ((bool)ViewBag.Flag)
        {
             <div id="note">
                <div class="notification_error">
                    @Html.ValidationSummary(false)
                </div>
            </div><!-- End notification -->
        }


in this i can't use viewbag.flag in view "book course"
Posted
Updated 23-Apr-14 21:57pm
v2

The data you put in the ViewBag/ViewData is only available during the life-cycle of the request within which you populated it. MVC does not have post backs. If you need something to persist over more than a single request, you should use Session.
 
Share this answer
 
1.ViewBag is a cache used to save data in the controller and use them in the view, and its value is lost between postbacks.

2.So you have to set the value "ViewBag.Flag" for all possible cases in your controller, not only for some if branches, for example you could set at beginning a default value!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900