當我將我的控制器 httpPost 顯示給我的錯誤 http 405 放入控制器時,我的表單有問題
我所做的每一步都是正確的
我的代碼
@model security.Models.ContactUs
<div class="contact_box">
<form id="Contact" asp-controller="Home" asp-action="Contact" method="POST">
<input type="text" asp-for="Name" placeholder="Your Name">
<input type="email" asp-for="Email" placeholder="Email">
<input type="text" asp-for="PhoneNumber" placeholder="Phone Number">
<input type="text" asp-for="Text" placeholder="Message">
<button type="submit">Contact Us</button>
</form>
</div>
和我的控制器
namespace security.Controllers
{
public class HomeController : Controller
{
DBContext db;
public HomeController(DBContext context)
{
db = context;
}
//private readonly ILogger<HomeController> _logger;
//public HomeController(ILogger<HomeController> logger)
//{
// _logger = logger;
//}
[HttpGet]
public IActionResult Index()
{
CollectionsData model = new CollectionsData();
model.Offers = GetOffers();
model.Services = GetServices();
model.News = GetNews();
model.Team = GetSecurityTeam();
return View(model);
}
[HttpPost]
public IActionResult Contact(ContactUs model)
{
db.Contactus.Add(model);
db.SaveChanges();
return RedirectToAction("Index");
}
public IActionResult Services( Sevices model)
{
var ModelServices = db.Sevices.ToList();
return View(ModelServices);
}
//[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
//public IActionResult Error()
//{
// return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
//}
public IEnumerable<Offers> GetOffers()
{
return db.Offers.ToList();
}
public IEnumerable<Sevices> GetServices()
{
return db.Sevices.ToList();
}
public IEnumerable<ourNews> GetNews()
{
return db.News.ToList();
}
public IEnumerable<SecurityTeam> GetSecurityTeam()
{
return db.Team.ToList();
}
}
}
但是當我洗掉 httppost 時一切正常,除了將表單發送到資料庫
我不知道為什么頁面沒有加載它給出 HTTP ERROR 405
謝謝
uj5u.com熱心網友回復:
您必須在表單內放置一個提交按鈕
<form asp-controller="Home" asp-action="Contact" method="POST">
....your code
<button type="submit">Contact Us</button>
</form>
或者由于某種原因,有時這效果更好
@using (Html.BeginForm("Contact", "Home", FormMethod.Post))
{
....your code
<button type="submit">Contact Us</button>
}
并且您需要 Contact get 操作來創建聯系人視圖
[HttpGet]
public IActionResult Contact()
{
var model= new ContactUs();
return View(model)
}
uj5u.com熱心網友回復:
嘗試這個
<form id="contact" method="POST" asp-action="Contact">
...
</form>
uj5u.com熱心網友回復:
return View()從 HTTP 動詞的角度來看沒有任何意義。您的表單正確發布,但是當您回傳視圖時,您需要更改它以重定向到您的 GET 操作。我不知道你的整個控制器是什么樣的,但我懷疑這是命名的問題。
嘗試
return RedirectToAction(“VIEWNAME”)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/493136.html
標籤:asp.net-mvc asp.net 核心 iis asp.net-core-mvc iis-express
