MVC在视图中发布局部视图并返回到具有局部视图的视图

时间:2015-01-16 15:07:50

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

我的主视图使用局部视图在单击按钮时显示模态。当发布视图中的局部视图时,我希望仍然显示包含视图的局部视图。但部分视图上的消息表明消息已提交。但似乎总是将消息返回到空白屏幕,并在其上显示返回消息。主视图位于与局部视图不同的控制器上。关于我可能做错的任何想法

主视图

More html here
@Html.Partial("_SendMessagePartial", message);
More html here

部分视图如下所示

@model MyProject.Models.InternalMessage

<div id="messageform">
    <!-- Modal -->
    @using (Ajax.BeginForm("Create", "InternalMessage", new AjaxOptions() { HttpMethod = "Post", UpdateTargetId = "messageform" }))
    {
        @Html.AntiForgeryToken()
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">Enquiry About @Model.RelatedCar.VehicleMake.Make, @Model.RelatedCar.VehicleModel.Model</h4>
                    </div>
                    <div class="modal-body">
                        @Html.LabelFor(i => i.Message)
                        @Html.TextAreaFor(i => i.Message)
                        @Html.ValidationMessageFor(i => i.Message)

                        @Html.HiddenFor(model => model.VehicleID)

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <input type="submit" value="Create" class="btn btn-primary" />
                    </div>

                </div>
            </div>
        </div>
    }
</div>

部分视图控制器的帖子

[HttpPost]
[ValidateAntiForgeryToken]
public string Create([Bind(Include = "ID,FromUserID,ToUserID,Message,DateSent,DateRead,Read,VehicleID,RelatedCompanyID")] InternalMessage internalmessage)
{
    if (ModelState.IsValid)
    {
        db.InternalMessage.Add(internalmessage);
        db.SaveChanges();
        return "You message has been sent to the vehicle owner";
    }

    return null;
}

0 个答案:

没有答案