简体   繁体   English

在ASP.NET MVC中,通过Ajax提交表单时如何返回不同的视图?

[英]In ASP.NET MVC, how do I return a different view when submitting a form via ajax?

I am attempting to create a message board in ASP.NET MVC. 我正在尝试在ASP.NET MVC中创建一个留言板。 I have two partial views, one to display a message (this is recursive and will display all child... messages as well), and one to display a form to submit new messages. 我有两个局部视图,一个显示一个消息(这是递归的,并且还将显示所有子...消息),另一个显示一个提交新消息的表单。 When a message gets posted, I want the form to submit via ajax, and return the partial view to display a message (the message that was just posted). 发布消息时,我希望表单通过ajax提交,并返回部分视图以显示消息(刚刚发布的消息)。

This is the partial view for displaying the form (NewMessage) 这是用于显示表单(NewMessage)的局部视图

@model Intranet.Entities.ForumRepository.Message

<div id="@Html.Raw("formbox" + Model.ParentID)">

@using (Ajax.BeginForm("NewMessage", new AjaxOptions { UpdateTargetId = "formbox" + Model.ParentID })) {    
    @Html.TextAreaFor(m => m.Text, new { @class = "responsebox" })
    @Html.HiddenFor(m => m.ParentID)
    <br /><input type="submit" value="Save Comment" class="savebutton" />
}

</div>

And its submit method 及其提交方法

[HttpPost]
public ActionResult NewMessage(ForumRepository.Message message) {
    if (ModelState.IsValid) {

        RouteData.Values.Add("Responses", message);
        //message.SaveMessage();

        return PartialView("DisplayMessage", message);
    } else {
        return PartialView(message);
    }

}

When I attempt to submit the form, the form view doesn't get replaced with the DisplayMessage view. 当我尝试提交表单时,表单视图不会被DisplayMessage视图替换。 It keeps showing the form. 它一直显示表格。 Running in debug mode shows that the backend code is getting called. 在调试模式下运行表明后端代码正在被调用。

I'm fairly certain that it has something to do with the fact that the div that the ajax code is using to redisplay is inside the NewMessage view (it can't replace its own container) but I have no idea how to set this up so that it will work. 我相当确定这与ajax代码用来重新显示的div在NewMessage视图内部(它不能替换其自己的容器)有关,但是我不知道如何设置它这样它才能起作用。


As requested, here is some rendered HTML 根据要求,这是一些渲染的HTML

<div id="formbox0">

<form action="/EventList/NewMessage/Q6UJ9A00T49L" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#formbox0" id="form0" method="post"><textarea class="responsebox" cols="20" data-val="true" data-val-required="The Text field is required." id="Text" name="Text" rows="2">
</textarea><input data-val="true" data-val-number="The field ParentID must be a number." data-val-required="The ParentID field is required." id="ParentID" name="ParentID" type="hidden" value="0" />    <br /><input type="submit" value="Save Comment" class="savebutton" />
</form>
</div>

I prefer to avoid the AjaxBeginForm method and like to write handwritten and Clean jQuery code. 我更喜欢避免使用AjaxBeginForm方法,并且喜欢编写手写和Clean jQuery代码。

I am giving a css class (commentItem) to the container div so that i can use it in my jQuery selector later. 我给容器div一个CSS类(commentItem),以便以后可以在我的jQuery选择器中使用它。

@model Intranet.Entities.ForumRepository.Message

<h3> Enter your comment</h3>
<div id="formbox@Model.ParentID" class="commentItem">   
  @using(Html.BeginForm())
  {
     @Html.TextAreaFor(m => m.Text, new { @class = "responsebox" })
     @Html.HiddenFor(m => m.ParentID)
     <input type="submit" value="Save Comment" class="savebutton" />
  }     
</div>
<script type="text/javascript">
  $(function(){
    $(".savebutton").click(function(e){
       var item=$(this);
       e.preventDefault();

       $.post("@Url.Action("NewMessage","EventList")",
                         item.closest("form").serialize(),function(data){

                   item.closest("div.commentItem").html(data);

       });

    });

  });

</script>

This code will replace the existing form (or whatever inside the container div) with the content received from your Action method. 此代码将用从您的Action方法接收的内容替换现有的表单(或容器div中的任何内容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM