【发布时间】: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