2014-09-19 48 views
2

我有一个引导模式。当我单击提交按钮时,页面会刷新并丢失模式。我想在提交按钮后单击以显示模式以显示模式成功消息或错误消息。我是MVC的新手,我无法弄清楚。使用Ajax刷新ASP.NET MVC中的引导模式

这是我的模态

<div class="modal fade" id="signin_modal"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class=" modal-header"> 
       Create An Account 
      </div> 

      <div class="modal-body"> 

       @using (Html.BeginForm("Login", "Home", FormMethod.Post)) 
       { 
        <table> 


         <tr><td>@Html.Label("username", null, new { style = " font-size:12px; font-weight:normal; font-family:consolas; " })</td></tr> 
         <tr><td>@Html.TextBox("name", null, new { style = " width:200px; height:30px; " })</td></tr> 


         <tr> </tr> 

         <tr><td>@Html.Label("password", null, new { style = " font-size:12px; font-weight:normal; font-family:consolas; " })</td></tr> 
         <tr><td>@Html.TextBox("password", null, new { style = " width:200px; height:30px " })</td></tr> 

        </table> 
       } 

      </div> 

      <div class="modal-footer"> 
       <button type="submit" class="btn btn-success">Sign in</button> 
       @*<button type="reset" class="btn btn-warning">Reset</button>*@ 
       <button type="reset" class="btn btn-default">Reset</button> 
      </div> 

     </div> 
    </div> 
</div> 

回答

1

我知道这是一个老问题,但是这是我会怎么做。

提供您使用强类型的视图模型,你可以添加一个名为IsModalShown

public class Foo 
{ 
    public bool IsModalShown { get; set; } 
} 

渲染这是一个隐藏在你的视图属性即

@Html.HiddenFor(m => m.IsModalShown) 

当模式被打开将隐藏值设置为true,这将使模态状态被重新发回到控制器动作,即

$('#signin_modal').on('show.bs.modal', function (e) { 
    $('#IsModalShown').val(true); 
}) 

请注意以上将取决于你所使用的自举的版本,但也有在官方网站上

那么其他文档添加以下到你的观点,即自动弹出起来

$(function(){ 
    @if(Model.IsModalShown) 
    { 
     $('#signin_modal').modal('show'); 
    } 
}); 

的弹出窗口中显示的其他字段也可以使用模型中的属性进行设置。