【问题标题】:Refresh partial view content when submit button clicked单击提交按钮时刷新部分视图内容
【发布时间】:2014-10-28 09:52:38
【问题描述】:

我有一个强类型视图,它有一个表单,当单击提交按钮时,我会在其中放置一个字段显示时间我把主视图。当调用索引操作方法时,主视图被渲染。我想在保存新条目时更新部分视图而不重新加载整个页面,只需刷新部分页面。

这是我的看法:

@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);
    }

我的问题:

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

我想更新部分视图而不重新加载整个页面如何实现?

【问题讨论】:

  • 您需要使用ajax提交表单(不是普通提交)并返回一个可以添加到DOM的部分视图
  • @StephenMuecke ,我将表单提交为:using (Ajax.BeginForm("Index", "TheaterShowTime", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "theaterShowList " } )) { @Html.AntiForgeryToken() @* 我的控件去 hre *@} 但这仍然会返回部分视图,只返回我需要在操作中更改的部分
  • 你问题中的代码显示 Html.BeginForm() 而不是 Ajax.BeginForm() 所以不确定你在说什么

标签: c# asp.net-mvc


【解决方案1】:

就像 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);
    }

这解决了我的问题

【讨论】:

  • 脚本对我来说不能正常工作。 Action 会重新加载漏洞页面并仅呈现部分视图。你知道为什么会这样吗?谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 2014-11-20
  • 2017-10-16
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多