开发者

Posting a form with anchor link at the end?

开发者 https://www.devze.com 2022-12-22 17:33 出处:网络
I have a form for comments like the one below but after the form is posted I wish to navigate to http://www.myurl.com/mypage#commentform but I don\'t know how to do this.

I have a form for comments like the one below but after the form is posted I wish to navigate to http://www.myurl.com/mypage#commentform but I don't know how to do this. Instead of changing my form maybe there is a way to return a View with my model and add #commentform to my url?

<div id="commentform">
    <h2>Leave a comment</h2>
    <% using (Html.BeginForm("Comment","Post", FormMethod.Post)) %>
    <% { %>
        <div>
            <%=Html.EditorFor(post => post.Comment) %>
            <div class="editor-button">
                <input type="submit" value="Comment" />
            </div>
        </div>            
    <% } %>开发者_开发百科;    
</div>


It seems you are trying to keep the position of the page on the same spot after submit. This could be achieved using an AJAX form.

<div id="commentform">
    <h2>Leave a comment</h2>
    <% using (Ajax.BeginForm("Comment", "Post", new AjaxOptions { UpdateTargetId = "Comment" })) {%></font></font> 
    <% { %>
        <div>
            <%=Html.EditorFor(post => post.Comment) %>
            <div class="editor-button">
                <input type="submit" value="Comment" />
            </div>
        </div>            
    <% } %>    
</div>


This is how I'd do it.

<a name="commentform"></a>
<div id="commentform">
    <h2>Leave a comment</h2>
    <% using (Html.BeginForm("Comment#commentform","Post", FormMethod.Post)) %>
    <% { %>
        <div>
            <%=Html.EditorFor(post => post.Comment) %>
            <div class="editor-button">
                <input type="submit" value="Comment" />
            </div>
        </div>            
    <% } %>    
</div>

I'm assuming you are not doing any redirects on success and that your url in your question corresponds to the page you want to access.

0

精彩评论

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

关注公众号