2011-06-08 66 views
1

我有一個頁面ProjectField.cshtml其中包含項目中的所有字段。 在HTML頁面我是這樣的:如何使用表單字段呈現頁面並在另一頁上驗證這些字段

@{ 
    WebSecurity.RequireAuthenticatedUser(); 

    // Initialize general page variables 
    var name = Request.Form["name"]; 
    var description = Request.Form["description"]; 
    int groupId; 
    if (Request.Form["groupId"] != null) 
    { 
     groupId = Request.Form["groupId"].AsInt(); 
    } 
    else 
    { 
     groupId = -1; 
    } 

    var menutitle = Request.Form["menutitle"]; 
} 
<div> 
    <div class="row-project"> 
     @Html.Label("Grupo: ", "group") 
    </div> 
    <select id="group" name="group"> 
     <option value="-1" @{ if(groupId < 0) { <text>selected="true"</text> } } label="--Selecione--">--Selecione--</option> 
     <option value="0" @{ if(groupId == 0) { <text>selected="true"</text> } } label="Residencial">Residencial</option> 
     <option value="1" @{ if(groupId == 1) { <text>selected="true"</text> } } label="Comercial">Comercial</option> 
     <option value="2" @{ if(groupId == 2) { <text>selected="true"</text> } } label="Institucional">Institucional</option> 
     <option value="3" @{ if(groupId == 3) { <text>selected="true"</text> } } label="Mostras">Mostras</option> 
    </select> 
    @Html.ValidationMessage("group") 
</div> 

<div> 
    <div class="row-project"> 
     @Html.Label("Projeto: ", "project") 
    </div> 
    @Html.TextBox("project", name) 
    @Html.ValidationMessage("project") 
</div> 

<div> 
    <div class="row-project"> 
     @Html.Label("Título Menu: ", "menutitle") 
    </div> 
    @Html.TextBox("menutitle", menutitle) 
    @Html.ValidationMessage("menutitle") 
</div> 

<div> 
    <div class="row-project"> 
     @Html.Label("Descrição: ", "description") 
    </div> 
    @Html.TextArea("description", description, 4, 35, null) 
    @Html.ValidationMessage("description") 
</div> 


<div class="upload"> 
    <div> 
    @Html.Label("Imagens: ", "images") 
    </div> 
    @FileUpload.GetHtml(
     initialNumberOfFiles:1, 
     allowMoreFilesToBeAdded:true, 
     includeFormTag:true, 
     addText:"Adicionar", 
     uploadText:"Salvar") 
    @Html.ValidationMessage("images") 
</div> 
<div class="row center-button clear"> 
    <!--<input type="submit" name="action" value="Salvar" title="Salvar"/>--> 
</div> 

在我的網頁Create.cshtml我使用的方法RederPage渲染頁ProjectFields.cshtml騎HTML。 請參閱完整代碼:

@{ 
     WebSecurity.RequireAuthenticatedUser(); 

     Layout = "~/Shared/_Layout.cshtml"; 
     Page.Title = "Novo Projeto"; 

     // Initialize general page variables 
     var name = Request.Form["name"]; 
     var description = Request.Form["description"]; 
     int groupId = Request.Form["groupId"].AsInt(); 
     var menutitle = Request.Form["menutitle"]; 

     if (IsPost) 
     { 
      // Validate 
      if (name.IsEmpty()) 
      { 
       ModelState.AddError("project", "Nome do projeto é obrigatório."); 
      } 

      if (menutitle.IsEmpty()) 
      { 
       ModelState.AddError("menutitle", "Título do menu é obrigatório."); 
      } 

      if (groupId < 0) 
      { 
       ModelState.AddError("group", "Grupo é obrigatório."); 
      } 

      if (Request.Files.Count <= 0 || Request.Files[0].ContentLength <= 0) 
      { 
       ModelState.AddError("images", "Insira pelo menos uma imagem."); 
      } 

      // Confirm there are no validation errors 
      if (ModelState.IsValid) 
      { 
       //CODE FOR SAVE DATA... 
      } 
     } 

} 

@if (!ModelState.IsValid) 
{ 
    @Html.ValidationSummary("Houve um problema no cadastro de projeto", true, new{ @class="error" }) 
} 
<form id="contact" method="post" enctype="multipart/form-data" action="@Href(@"~\Project\Create")"> 
    <fieldset> 
     @RenderPage(Href("~/Project/Views/ProjectField.cshtml")) 
    </fieldset> 
</form> 

@BindHelpers.Link(Href("~/Project/Index"), "Lista de projetos", "projects", "projects-link") 

但是,當我做了後,它並不對數據進行驗證。 我注意到,它運行後形式Create.cshtml,然後立即發佈ProjectField.cshtml而是將驗證字段相同Create.cshtml

+0

對不起,請再試一次。 – ridermansb 2011-06-08 23:13:21

+0

別忘了標題:)謝謝! – sarnold 2011-06-08 23:14:45

+0

對不起,我糾正了這個問題。 – ridermansb 2011-06-08 23:44:01

回答

0

不知道這是否會對你有幫助,但是我沒有看到表單標籤(或者幫助者創建一個,如果確實存在這樣的幫助者)。我錯過了什麼嗎?我確實看到了你的輸入類型「submit」標籤,但我看不到表單標籤(動作設置爲Create.cshtml)。

希望這會有所幫助!