【问题标题】:replace form after submit with ajax用ajax提交后替换表单
【发布时间】:2017-06-25 13:56:02
【问题描述】:

我设法让我的ajax 请求启动并运行,现在我想根据控制器操作返回的响应关闭表单。反正聊得够多了,我有以下几行:

控制器动作:

public IActionResult Create(MyviewModel model)
{
   if(!ModelState.IsValid)
   {
     return BadRequest(ModelState);
   }

   var r = _context.Save(model);

    if(r.IsError) 
       prepare error message and send them back

   return OK(r.Data);
}

剃须刀:

<form asp-controller="MyController" asp-action="Create" data-ajax" data-ajax-method="POST">
  <div class="form-group">
    <label asp-for="Email"></label>
    <input asp-for="Email" class="form-control">
  </div>
  <div class="form-group">
    <label asp-for="Comment"></label>
    <input asp-for="Comment" class="form-control">
   </div>
   <button type="submit" class="btn btn-default">Submit</button>
</form>

我可以在不刷新 URL 的情况下点击我的操作方法。我可以处理请求,这很好。但是我如何才能得到响应并采取相应的行动呢?我收集到 data-ajax-success-failure 可能是我的朋友,但我不知道怎么做。

我基本上想将成功的表单替换为:

<div class="alert alert-success" role="alert">Comment successfully submitted</div>.

如果出现错误,请读取从服务器返回的错误,并提醒用户。在这种情况下,请保留表单以防用户重试。

我是这个 javascript 世界的新手,谁能提供一个最小的工作解决方案或指出我正确的方向?

【问题讨论】:

    标签: javascript css ajax razor asp.net-core-mvc


    【解决方案1】:

    如果您愿意,可以远离 JavaScript 代码。

    如示例所示,将data-ajax-modedata-ajax-update 属性添加到表单标签中。

    在您设置正确的消息后,从您的控制器返回一个 PartialView。

    <div id="myDiv">
        @if (ViewBag.SuccessMessage != null)
        {
            <div class="alert alert-success" role="alert">@Html.Raw(ViewBag.SuccessMessage)</div>
        }
        else
        {
            <form asp-controller="MyController" asp-action="Create" data-ajax="true" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-update="#myDiv">
                <div class="form-group">
                    <label asp-for="Email"></label>
                    <input asp-for="Email" class="form-control">
                </div>
                <div class="form-group">
                    <label asp-for="Comment"></label>
                    <input asp-for="Comment" class="form-control">
                </div>
                @if (ViewBag.ErrorMessage != null)
                {
                    <div class="alert alert-danger" role="alert">@Html.Raw(ViewBag.ErrorMessage)</div>
                }
                <button type="submit" class="btn btn-default">Submit</button>
            </form>
        }
    </div>
    

    在您的表单发布操作中,设置正确的消息并返回与 PartialView 相同的视图。

    [HttpGet]
    public IActionResult Create()
    {
        return View();
    }
    
    [HttpPost]
    public IActionResult Create(MyviewModel model)
    {
       if(!ModelState.IsValid)
       {
           ViewBag.ErrorMessage = "Input not valid!";
           return PartialView(model);
       }
    
       var r = _context.Save(model);
    
        if(r.IsError) 
        {
           ViewBag.ErrorMessage = "Error when saving...";
           return PartialView(model);
        }
    
        ViewBag.SuccessMessage = "Created successfully!";
        return PartialView(model);
    }
    

    【讨论】:

    • 太棒了:)。在那里我交叉手指和脚趾,必须有非 js 解决方案。如果我理解得很好,我将不得不创建一个单独的partilaView 文件。第一次加载父视图时如何包含它,因为我需要初始表单?
    • 无需创建单独的PartialView。用答案的剃刀替换您的视图。在 Get 上,您返回 View,但在 post 上,您返回 PartialView。
    • 我更新了答案。我假设您的 View 名为 Create.cshtml,并且存在于与 Controller 同名的文件夹中。
    • @JoãoPereira 你知道我们是否有jquery.unobtrusive-ajax cdn 坐在某个地方或者(为什么微软不在docs.microsoft.com/en-us/aspnet/ajax/cdn/overview 上托管它)?
    • @edit 从来没有注意到它...我将我的库捆绑在解决方案中,因此在 CDN 中从未需要它。尽管如此,我发现它绑定到 MVC v3.0 here。它有点旧,但做了一些小改动......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    相关资源
    最近更新 更多