无法从控制器访问viewdata和viewbag来查看 [英] can't access viewdata and viewbag from controller to view
本文介绍了无法从控制器访问viewdata和viewbag来查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友
i无法通过viewdata或viewbag从控制器发送数据查看。
请帮我解决这个问题。
这是我的代码:
控制器
public ActionResult BookCourse(string Id)
{
services();
var item = _courseDetailsService.GetAll();
ViewBag.Code = Id.ToString();
ViewBag.Course = item;
TempData [code] = Id;
ViewBag.title =预订课程;
ViewBag.Flag = false;
return View();
}
[HttpPost]
[RecaptchaControlMvc.CaptchaValidator]
public ActionResult BookCourse(CourseBooking book,bool captchaValid,string captchaErrorMessage)
{
尝试
{
services();
string code = TempData [code]。ToString();
bool valid = true;
if(!captchaValid)
{
ViewBag.ErrorMessage =Invalid Entry *;
ModelState.AddModelError(Captcha,请正确输入验证文本);
有效= false;
}
if(book.FirstName == null || book.FirstName ==)
{
ModelState.AddModelError(FirstName,Enter名字);
有效= false;
}
if(book.LastName == null || book.LastName ==)
{
ModelState.AddModelError(LastName,输入姓氏) ;
有效= false;
}
if(book.EmailAddress == null || book.EmailAddress ==)
{
ModelState.AddModelError(EmailAddress , 输入电邮地址);
有效= false;
}
其他
{
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 invalid);
有效= false;
}
}
if(!valid)
{
ViewBag.Flag = true;
返回BookCourse(代码);
}
book.DateBooked = DateTime.Now;
_courseBookingService.Add(book);
SendNotificationMail(书籍);
}
catch(Exception ex)
{
_log.Log(ex);
}
返回RedirectToAction(谢谢);
}
查看(图书课程)
@if((bool)ViewBag.Flag)
{
< div id =note>
< div class =notification_error>
@ Html.ValidationSummary(false)
< / div>
< / div><! - 结束通知 - >
}
这个我不能在视图book course中使用viewbag.flag
解决方案
;
Regex re = new Regex(emailRegex);
if(!re.IsMatch(book.EmailAddress))
{
ModelState.AddModelError(电子邮件,电子邮件地址无效);
有效= false;
}
}
if(!valid)
{
ViewBag.Flag = true;
返回BookCourse(代码);
}
book.DateBooked = DateTime.Now;
_courseBookingService。添加(书籍);
SendNotificationMail(书籍);
}
catch(例外) ex)
{
_log.Log(ex);
}
返回RedirectToAction(谢谢);
}
查看(图书课程)
@if((bool)ViewBag.Flag)
{
< div id =note>
< div class =notification_error>
@ Html.ValidationSummary(false)
< / div>
< / div><! - 结束通知 - >
}
这个我不能在视图book course中使用viewbag.flag
您放入ViewBag / ViewData的数据仅在您填充它的请求的生命周期内可用。 MVC没有回发帖。如果你需要一些东西来坚持多个请求,你应该使用Session。
1.ViewBag
是一个用于缓存的缓存将数据保存在控制器中并在视图中使用它们,它的值在回发之间丢失。
2.所以你必须设置值ViewBag.Flag
对于控制器中的所有可能情况,不仅对于某些if
分支,例如您可以在开始时设置默认值值<!/ BLOCKQUOTE>
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"解决方案"; 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"
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.
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 someif
branches, for example you could set at beginning a default value!
这篇关于无法从控制器访问viewdata和viewbag来查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文