【问题标题】:why post method doesn't refresh the page in the action method?为什么 post 方法在 action 方法中不刷新页面?
【发布时间】:2022-11-12 19:22:36
【问题描述】:

我知道下面的 post action 方法是反模式的,但我仍然假设看到一个新页面,其中 Name 设置为 null。但是当我点击提交按钮时,页面没有重新加载,我仍然看到显示的旧名称,这是浏览器的事情还是 asp.net 核心框架的事情?

public class HomeController : Controller
{
   private IRepository repository;
   public HomeController(IRepository repo)
   {
      repository = repo;
   }

   // ...

   public IActionResult Create()  // create a Employer that has a name in the browser
   {
      return View();
   }

   [HttpPost]
   public IActionResult Create(Employee model)
   {
      model.Name = "";
      return View(model);  
   }
}

// view file:

@model Employee
@{
    ViewData["Title"] = "Create Employee";
}

<h2>Create Employee</h2>

<form asp-action="Create" method="post">
    <div class="form-group">
        <label asp-for="Id"></label>
        <input asp-for="Id" class="form-control" />
    </div>
    <div class="form-group">
        <label asp-for="Name"></label>
        <input asp-for="Name" class="form-control" />
    </div>
    <div class="form-group">
        <label asp-for="DOB"></label>
        <input asp-for="DOB" class="form-control" />
    </div>
    <div class="form-group">
        <label asp-for="Role"></label>
        <select asp-for="Role" class="form-control" asp-items="@new SelectList(Enum.GetNames(typeof(Role)))"></select>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

【问题讨论】:

  • 浏览器控制台中是否有任何错误?

标签: c# asp.net-core


【解决方案1】:

它可以重置您已响应的数据。所以基本上发送数据没有任何损失

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多