繁体   English   中英

单击提交按钮时刷新部分视图内容

[英]Refresh partial view content when submit button clicked

我有一个强类型的视图,该视图具有一种形式,当单击提交按钮时,我在其中放置了一个字段放映时间。我要将条目保存到数据库中,还将在我放置的局部视图中在数据库中显示该条目以及其他条目主要观点。 当调用Index操作方法时,呈现主视图。保存新条目时,我想更新部分视图,而无需重新加载整个页面,只需刷新部分页面即可。

这是我的看法:

@model Bookmany.Admin.Models.Theater.TheaterShowTimeViewModel

@{
    ViewBag.Title = "Theater showTimes / BookMany";
 }

<h3>Theater ShowTimes</h3>
 @using (Html.BeginForm())
 {
@Html.AntiForgeryToken()

<div class="form-horizontal">

    <hr />
    @Html.ValidationSummary(true)

    <div class="form-group">
        @Html.LabelFor(model => model.TheaterID, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.TheaterID, Model.AvailableTheaters)
            @Html.ValidationMessageFor(model => model.TheaterID)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ShowTimeName, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ShowTimeName)
            @Html.ValidationMessageFor(model => model.ShowTimeName)
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
  @Html.Partial("_TheaterShowTimeList", Model.TheaterShowTimeList)
</div>

这是我的部分观点:

@model IEnumerable<Bookmany.Core.Domain.TheaterShowTime>

<table class="table">
<tr>
    <th>
        Theater Name
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ShowTimeName)
    </th>
    <th></th>
</tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Theater.TheaterName)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.TheaterShowTimeID }) |
            @Html.ActionLink("Details", "Details", new { id = item.TheaterShowTimeID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.TheaterShowTimeID })
        </td>
    </tr>
}

我的动作方法:

    public ActionResult Index()
    {
        var model = new TheaterShowTimeViewModel();

        //Bind Theater dropdown with selected value
        model.AvailableTheaters = GetTheaters();
        model.TheaterShowTimeList = _theaterShowTimeService.GetAllTheaterShowTime();
        return View(model);

    }

    [HttpPost]
    public ActionResult Index(TheaterShowTimeViewModel model)
    {
        if (ModelState.IsValid)
        {
            InsertOrUpdateTheaterShowTime(model);
            model.TheaterShowTimeList=_theaterShowTimeService.GetAllTheaterShowTime();
            return PartialView("_TheaterShowTimeList", model.TheaterShowTimeList);
        }
        return View(model);
    }

我的问题:

当我在字段中输入一个值并提交表单时,现在保存的条目部分视图仅与更新后的列表一起返回

我想在不重新加载整个页面的情况下更新局部视图,该如何实现?

就像史蒂芬·穆克(Stephen Muecke)所说的那样,当单击提交按钮时,我使用Ajax发布了表单

<script type="text/javascript" >
$(function () {
$('form').submit(function () {
    if ($(this).valid()) {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                $('#ShowTimeName').val("");
                $('#theaterShowList').html(result);
            }
        });
    }
    return false;
  });
 });
</script>

在我的操作方法中返回部分视图:

    [HttpPost]
    public ActionResult Index(TheaterShowTimeViewModel model)
    {
        if (ModelState.IsValid)
        {
            InsertOrUpdateTheaterShowTime(model);
            model.TheaterShowTimeList=_theaterShowTimeService.GetAllTheaterShowTime();
            //return RedirectToAction("Index", new { id = model.TheaterID });
            //return Json(model.TheaterShowTimeList);
            return PartialView("_TheaterShowTimeList", model.TheaterShowTimeList);
        }
        return View(model);
    }

这解决了我的问题

暂无
暂无

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

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