2013-06-24 175 views
1

我很努力地看到如何保存一個頁面狀態而不做一個會話變種。 我有一個頁面,其中包含一個改變頁面狀態的按鈕,在發佈後,視圖可能返回,但處於錯誤狀態。我試過ViewBag,這不能在java範圍內設置。我需要一個解決方案,以節省的進行AuthType值..保存頁面狀態mvc4

@model Trakman_Portal_Administration.Models.Connection 

@ {

ViewBag.Title = "Create"; 
var conStringType = ViewBag.conStringType; 

}

@Scripts.Render("~/bundles/jquery") 

<script type='text/javascript'> 

var authType = 1 

$(document).ready(function() { 
    alert(authType); 
    if (authType == 0) { 

     $("#usernameLabel").hide(); 
     $("#usernameField").hide().find('input:text').val(""); 

     $("#passwordLabel").hide(); 
     $("#passwordField").hide().find('input:text').val(""); 

     //$("#initialCatalogLabel").show(); 
     //$("#initialCatalogField").show(); 

     document.getElementById("button1").textContent = "Intergrated"; 
     authType = 1; 
    } 
    else { 

     $("#usernameLabel").show(); 
     $("#usernameField").show(); 
     $("#passwordLabel").show(); 
     $("#passwordField").show(); 
     //$("#initialCatalogLabel").hide(); 
     //$("#initialCatalogField").hide(); 

     document.getElementById("button1").textContent = "SQL Authentication"; 
     authType = 0; 
    } 

    $("button").click(function() { 

     //$("#usernameField").attr("value",""); 
     //$("#passwordField").attr("value",""); 


     if (authType == 0) { 

      $("#usernameLabel").hide(); 
      $("#usernameField").hide().find('input:text').val(""); 

      $("#passwordLabel").hide(); 
      $("#passwordField").hide().find('input:text').val(""); 

      //$("#initialCatalogLabel").show(); 
      //$("#initialCatalogField").show(); 

      document.getElementById("button1").textContent = "Intergrated"; 
      authType = 1; 

     } 
     else { 

      $("#usernameLabel").show(); 
      $("#usernameField").show(); 
      $("#passwordLabel").show(); 
      $("#passwordField").show(); 
      //$("#initialCatalogLabel").hide(); 
      //$("#initialCatalogField").hide(); 

      document.getElementById("button1").textContent = "SQL Authentication"; 
      authType = 0; 

     } 
    }); 
}); 

<h2>Create</h2> 



@using (Html.BeginForm()) 
{ 
@Html.ValidationSummary(true) 

<fieldset> 
    <legend>connection</legend> 
    <div class="editor-label"> 
     Authentication Type 
    </div> 
    <div> 
     <button id="button1" value="Intergrated" name="intergrated" >SQL Authentication</button> 
    </div> 
    <br /> 
    <br /> 
    <br /> 

    <div class="editor-label"> 
     Connection Name 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.conName) 
     @Html.ValidationMessageFor(model => model.conName) 
    </div> 
    <div class="editor-label"> 
     Data Source 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.dataSource) 
     @Html.ValidationMessageFor(model => model.dataSource) 
    </div> 
    <div class="editor-label" id="initialCatalogLabel"> 
     Initial Catalog 
    </div> 
    <div class="editor-field" id="initialCatalogField"> 
     @Html.EditorFor(model => model.initialCatalog) 
     @Html.ValidationMessageFor(model => model.initialCatalog) 
    </div> 

    <div class="editor-label" id = "usernameLabel"> 
     Username 
    </div> 
    <div class="editor-field" id="usernameField"> 
     @Html.EditorFor(model => model.username) 
     @Html.ValidationMessageFor(model => model.username) 
    </div> 
    <div class="editor-label" id = "passwordLabel"> 
     Password 
    </div> 
    <div class="editor-field" id = "passwordField"> 
     @Html.EditorFor(model => model.password) 
     @Html.ValidationMessageFor(model => model.password) 
    </div> 

    <p> 
     <input type="submit" value="Create"/> 
     @{ 
if (!string.IsNullOrEmpty(ViewBag.error)) 
{ 
       <div style="color:Red"> 
        @ViewBag.error 
       </div>   
} 
     } 
    </p> 

</fieldset> 

}

回答

0

我很努力,看看如何保存頁面狀態而不進行會話變種

使用視圖模型保留整個後背上你的信息會做的伎倆。

public class ConnectionViewModel 
{ 
    public string AuthenticationType { get; set; } 
    public string ConnectionString { get; set; } 
    ... 
} 

public ActionResult Login() 
{ 
    // pass in defaults 
    return View(new ConnectionViewModel 
    { 
     AuthenticationType = "Windows", 
     ConnectionString = "..." 
    }); 
} 

[HttpPost] 
public ActionResult Login(ConnectionViewModel viewModel) 
{ 
    // pass view model back into view to retain values 
    return View(viewModel); 
} 
+0

是的,我認爲這會工作,只是想我可能已經錯過了MVC的新東西。 –