我有一个评论表单,如下所示,但在发布表单后,我希望导航到http://www.myurl.com/mypage#commentform,但我不知道如何执行此操作。 可能有一种方法可以使用我的模型返回视图并将#commentform添加到我的网址,而不是更改我的表单?
<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>
答案 0 :(得分:0)
似乎您在提交后试图将页面的位置保持在同一位置。这可以使用AJAX表单来实现。
<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>
答案 1 :(得分:0)
我就是这样做的。
<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>
我假设您没有在成功时进行任何重定向,并且您的问题中的网址与您要访问的网页相对应。