2017-07-03 33 views
0

我是全新的ASP.NET MVC网页开发。 我试图建立一个网站,基本上可以访问数据库,可以由用户填写。ViewBag内容未被可视化

好吧,一切都很好,那部分工作正常。我正在努力的是,只要用户将数据插入数据库,我想显示一条消息,说他的插入已成功完成,或类似的事情。我想在表单面板中显示出来。

这是我到目前为止的代码:

这是该方法被调用,当用户点击提交按钮:

[HttpPost] 
public ActionResult Create(PersonModels person) 
{ 
    try 
    { 
     // TODO: Add insert logic here 
     //Adding to database and holding the response in the viewbag. 
     string strInsertion = ConnectionModels.insertPerson(person); 
     ViewBag.InsertionResult = strInsertion; 

     return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View("Index"); 
    } 
} 

这是我的索引行动:

public ActionResult Index() 
{ 
    return View(); 
} 

最后,这是我想要在我的index.cshtml中的内容:

<div> 
    <label> 
     @ViewBag.InsertionResult 
    </label> 
</div> 

我不会发布它完全'因为它会相当广泛。但那是在表单面板的div下面。

我希望你能帮我一把。

在此先感谢! :)

+1

确切的问题在这里回答:https://stackoverflow.com/questions/14497711/set-viewbag-before-redirect –

回答

3

在重定向到另一个操作时,您无法传递ViewBag值。如果你是在同一个控制器可以使用TempData传递会话中的值,否则你可以传递消息作为参数来RedirectionResult象下面这样:

return RedirectToAction("Index", new {message="Your Message"}); 

,然后拿回来这样的:

public ActionResult Index(string message) 
{ 
    ViewBag.ViewBag.InsertionResult = message; 
    return View(); 
} 

这是如何传递消息,但我会建议像这样的一般方法:

使用BaseController,所有控制器从这个继承:

在这里,您可以制定一个自定义逻辑,如何处理全局消息,如错误消息,通知消息,信息消息等。

对于您需要创建一个模型,如下图所示:

我保持简单在这里:

public class GlobalMessage 
{ 
    public string Message { get;set;} 
    public AlertType AlertType {get;set;} 
} 
public enum AlertType 
{ 
    Success, Info, Error, Danger//etc 
} 

BaseController你有这样的事情:

public abstract class BaseController : Controller 
{ 
    protected GlobalMessage GlobalMessage; 
    protected override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     if (filterContext.Result is ViewResult) 
     { 
      if (GlobalMessage!= null) 
      { 
       filterContext.Controller.ViewBag.GlobalMessage = GlobalMessage; 
      } 
      else 
      { 
       GlobalErrorViewModel globalErrorModelView = TempData["GlobalMessage"] as GlobalMessage; 

       if (globalErrorModelView != null) 
       { 
        filterContext.Controller.ViewBag.GlobalErrorViewModel = globalErrorModelView; 
       } 
      } 
     } 
     base.OnActionExecuted(filterContext); 
    } 

} 

在这一刻,你只需要注册新的GlobalMessageTempdata如下:

public PeopleController : BaseController 
{ 
    [HttpPost] 
    public ActionResult Create(PersonModels person) 
    { 
     try 
     { 
      // TODO: Add insert logic here 
      //Adding to database and holding the response in the viewbag. 
      string strInsertion = ConnectionModels.insertPerson(person); 
      TempData["GlobalMessage"] = new GlobalMessage{ AlertType = AlertType.Info, Message = "You have successfully added a new person" } 

      return RedirectToAction("Index"); 
     } 
     catch 
     { 
      return View("Index"); 
     } 
    } 
} 

当年这里是最后一步如何在视图中显示的数据:

我个人使用弹出窗口或模态窗口,要做到这一点:例如在bootstrapp你会写是这样的:

GlobalMessage globalMessage = ViewBag.GlobalMessage as GlobalMessage; 
    @if (globalMessage != null) 
    { 
     <!-- Modal --> 
     <div class="modal fade" id="globalMessage" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
      <div class="modal-dialog"> 
       <div class="modal-content [email protected] .AlertType.ToString().ToLower() remove-border-radius"> 
        <div class="modal-header panel-heading"> 
         <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
        </div> 
        <div class="modal-body"> 
         <p class="h-text-primary">@Html.Raw(globalMessage .Message)</p> 
        </div> 
        <div class="modal-footer"> 
         <button type="button" class="btn [email protected] .AlertType.ToString().ToLower() remove-border-radius" data-dismiss="modal">Close</button> 
        </div> 
       </div><!-- /.modal-content --> 
      </div><!-- /.modal-dialog --> 
     </div><!-- /.modal --> 
    } 

触发如果有消息的模式:

@if (globalMessage != null) 
{ 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('#globalMessage').modal('show'); 
     }); 
    </script> 
} 

这个例子是为了告诉你如何使一个系统来显示不同的信息。无论你想要什么,总之,

+0

党,这真的很有帮助!非常感谢! :D – RottenCheese

+0

我很高兴我帮助:) –

0

ViewBag and ViewData对象在重定向后将返回null值。他们的内容仅在当前请求期间存活。

还有另一种属性,TempData,其内容将承受当前和随后请求,但只有后续请求。但是,这意味着当您拨打RedirectToAction时,您可以使用它传递数据。

[HttpPost] 
public ActionResult Create(PersonModels person) 
{ 
    try 
    { 
     // TODO: Add insert logic here 
     //Adding to database and holding the response in the viewbag. 
     string strInsertion = ConnectionModels.insertPerson(person); 
     TempData[InsertionResult] = strInsertion; 

     return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View("Index"); 
    } 
} 

[HttpGet] 
public ActionResult Index() 
{ 
    string strInsertion = TempData[InsertionResult]; 
    return View("Index", new { InsertionResult = strInsertion }); 
} 

<label> 
    @Model.InsertionResult 
</label> 

一个很好的解释在这里给出:http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications/

另一种做法将是您的Index视图采取包含在数据库中创建的人HttpPost的结果可选视图模型。您可以使用该模型来填充标签的内容。强类型模型FTW!