【问题标题】:ASP.NET Core MVC : Implement Save and New functionalityASP.NET Core MVC:实现保存和新建功能
【发布时间】:2021-12-15 01:49:07
【问题描述】:

我正在尝试创建“保存”、“保存并新建”和“取消”功能。

根据我的理解,如果按钮的值为“Save”,我应该获取按钮值并保存,如果按钮值为“SaveNew”,则保存并返回创建操作。

这就是我的代码现在的样子:

型号

    [Key]
    public int Id { get; set; }
    
    [Required]
    public string Profile { get; set; }

查看

    <form asp-action="CreatePost">
        <div class="row g-3">
            <div class="form-group col-sm-6">
                <label asp-for="Profile" class="control-label"></label>
                <input asp-for="Profile" class="form-control" />
                <span asp-validation-for="Profile" class="text-danger"></span>
            </div>
        </div>
        <div class="d-flex flex-row justify-content-center">
            <button type="submit" value="Save" class="w-25 btn btn-primary btn-lg me-5">Save</button>
            <button type="submit" value="SaveNew" class="w-25 btn btn-primary btn-lg me-5">Save+New</button>
            <a class="w-25 btn btn-secondary btn-lg me-5" asp-action="Index">Cancel</a>
        </div>
    </form>

控制器

    [HttpPost]
    public IActionResult CreatePost(Profile obj)
    {
        _db.UserProfiles.Add(obj);
        _db.SaveChanges();
        return RedirectToAction("Index")            
    }

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow。 SO是问答网站。当涉及到How to Ask 和这个问题checklist 时,您可能会发现阅读网站help center 开始。我建议你继续你的开发;如果您遇到问题 - 请随时提出问题
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: asp.net-core .net-core asp.net-core-mvc .net-5


【解决方案1】:

在浏览了几个小时的论坛后,我终于明白了如何做到这一点。我必须在视图和控制器中进行修改。这是我上面答案的解决方案。

我修改了视图内的表单。必须先更改和分配按钮的名称和值。

查看:

<form asp-action="CreatePost">
        <div class="row g-3">
            <div class="form-group col-sm-6">
                <label asp-for="Profile" class="control-label"></label>
                <input asp-for="Profile" class="form-control" />
                <span asp-validation-for="Profile" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group d-flex flex-sm-shrink-1 justify-content-center">
            <button type="submit" name="ActionType" value="Save" class="btn btn-primary">Save</button>
            <button type="submit" name="ActionType" value="SaveNew" class="btn btn-primary mx-3">Save+New</button>
            <a class="btn btn-secondary" asp-action="Index">Cancel</a>
        </div>
</form>

然后我不得不修改控制器并设置一个if语句如下:

控制器

        [HttpPost]
        public IActionResult CreatePost(Profile obj, string ActionType)
        {
            if (ModelState.IsValid)
            {
                if (ActionType == "SaveNew")
                {
                    _db.Profiles.Add(obj);
                    _db.SaveChanges();
                    return RedirectToAction("Create");
                }
                else if (ActionType == "Save")
                {
                    _db.Profiles.Add(obj);
                    _db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            return View("Create",obj);

        }

因此,对应于每个按钮值,控制器将导航到适当的操作。如果您觉得这有帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2020-01-20
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多