2017-07-03 28 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!