部分视图作为整页返回

时间:2019-04-23 07:27:04

标签: c# jquery ajax asp.net-mvc model-view-controller

我的局部视图将我重定向到另一个页面。

Index.cshtml

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js") type="text/javascript"></script>
@using (Ajax.BeginForm("SendChat", new AjaxOptions { UpdateTargetId = "messages" }))
{
<div>
    @Html.TextBoxFor(m => m.Message.Text)
    @Html.TextBoxFor(m => m.Client.client_id, new { @Value = Model.Client.client_id, @class = "hidden" })
</div>
    <div>
        <input type="submit" value="Send message" />
    </div>
}

<div class="messages">
    @{Html.RenderPartial("~/Views/Partial/Chat.cshtml", Model.Messages);}
</div>

局部视图Chat.cshtml

@using OPP.Web.Models
@model List<OPP.Web.Models.Message>


    @for (int i = 0; i < Model.Count; i++)
    {
        Message message = Model[i];
        if (message.OwnershipId == 1)
        {
            <p style="text-align:right">
                @message.Text
            </p>
        }

        if (message.OwnershipId == 2)
        {
            <p style="text-align:left">
                @message.Text
            </p>
        }
    }

我的控制器:

[HttpPost]
        public ActionResult SendChat(Message message, int id = 5)
        {
            WorkoutViewModel workoutViewModel = new WorkoutViewModel();
            workoutViewModel.Client = GetClient(id);
            int trainerId = workoutViewModel.Client.TrainerId ?? default(int);
            workoutViewModel.Chat = GetChat(workoutViewModel.Client.client_id, trainerId);

            message.ChatId = workoutViewModel.Chat.Id;
            if (User.IsInRole("Admin"))
                message.OwnershipId = 1;
            else if (User.IsInRole("Client"))
                message.OwnershipId = 2;
            List<Message> messages = new OPPDBContext().Messages.ToList();
            Message tmp = messages[messages.Count - 1];
            message.Order = tmp.Order + 1;
            message.Id = tmp.Id + 1;
            try
            {
                using (OPPDBContext context = new OPPDBContext())
                {
                    context.Messages.Add(message);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
                return PartialView("~/Views/Partial/Chat.cshtml", messages);
        }

好像我已经准备好所有需要的东西。有什么事吗脚本下有绿色的波浪线,没有错误代码,仅此而已。不知道该怎么办。

1 个答案:

答案 0 :(得分:0)

我认为,您需要按如下所示从控制器返回索引视图:

return View("Index", workoutViewModel);