开发者

ASP.NET MVC 3 validation problem

开发者 https://www.devze.com 2023-04-03 17:21 出处:网络
I have 2 or more forms with different one hidden value like: <div class=\"comment-body\"> <% using (Html.BeginForm(Model.ActionName, \"Home\", Model.RouteValues, FormMethod.Post, new { id =

I have 2 or more forms with different one hidden value like:

    <div class="comment-body">
        <% using (Html.BeginForm(Model.ActionName, "Home", Model.RouteValues, FormMethod.Post, new { id = "FormAddComment", name = "FormAddComment" }))
           { %>
        <% = Html.ValidationSummary(false) %>
        <fieldset>
            <% if (Model.CommentParentID != null)
               {
                   htmlAttributes = new { placeholder = "Add a reply...", id = "Comment" };
                   postButtonTitle = "Post Reply";
            %>
            <input type="hidden" name="CommentParentID" id="CommentParentID" value="<% = Model.CommentParentID.ToString() %>" />
            <%} %>
            <% = Html.TextAreaFor(model => model.Comment, htmlAttributes)%>
            <% = Html.ValidationMessageFor(model=>model.Comment) %>
            <input type="submit" value="<% = postButtonTitle %>" class="small blue awesome noborder"
                border="0" />
            <% if开发者_运维百科 (Model.CommentParentID != null)
               {%>
            <a class="hide-sub-comment" href="javascript:void(0);">Cancel</a>
            <%} %>
        </fieldset>
        <%} %>
    </div>

Problem is when I try to validate entered value I got the validator message twice. When I add text and click "post" again - one validator is hidden, but page is not valid yet. Why and how to solve it? Thanks


Modifying answer since you changed the question.

You could do something like this

<script type="text/javascript">
    $(document).ready(function () {            
        $("#NotHiddenComment").change(function () {
            var temp = $("#NotHiddenComment").val();
            $("#HiddenCommentID").text(temp);
        });
    });
</script>

This will make sure both fields have the same value so you do not get validation errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消