如何使用表单字段呈现页面并在另一页上验证这些字段

时间:2011-06-08 23:03:25

标签: html forms post razor webmatrix

我有一个 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方法来运行html以呈现页面 ProjectFields.cshtml 。 请参阅完整代码:

@{
        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

1 个答案:

答案 0 :(得分:0)

不确定这是否会对你有所帮助但是,我没有看到表格标签(或帮助者制作一个,如果确实有这样的帮助者)。我错过了什么吗?我确实看到你的输入类型为“submit”标签,但我看不到表单标签(操作设置为Create.cshtml)。

希望这有帮助!