I have a page ProjectField.cshtml which contains all the fields in a project. In the HTML page I have is this:
@{
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>
In my page Create.cshtml I ride the html using the method RederPage to render page ProjectFields.cshtml . See the complete code:
@{
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")
But when I make the post, it does not validate the data. I noticed that it runs the post form Create.cshtml and then immediately post the ProjectField.cshtml but that will validate the fields is the same Create.cshtml.
not sure if this will help you but, I don't see a form tag (or the helper to make one, if indeed there is such a helper). Am I missing something? I do see your input type "submit" tag, but I couldn't see a form tag (with action set to Create.cshtml).
Hope this helps!
精彩评论