【问题标题】:How to render a page with form fields and validate these fields on another page如何使用表单字段呈现页面并在另一个页面上验证这些字段
【发布时间】:2011-06-08 23:03:24
【问题描述】:

我有一个页面 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 。 查看完整代码:

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

【问题讨论】:

  • 别忘了标题 :) 谢谢!
  • 再次抱歉,我更正了问题。

标签: html forms post razor webmatrix


【解决方案1】:

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

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2021-08-09
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 2018-09-25
    • 2012-03-19
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    相关资源
    最近更新 更多