0
私はasp.net razor engineを使用しています。私は削除ボタンがあり、それを初めて押すと、それは動作します。 2回目にURLが機能を繰り返し、動作しません。URLが反復しているようです
は、これは私が削除使う二度目です。 URLがdel/delであることに注意してください。その2番目のデルを避けようとしています。ここで
は私のコントローラである。ここ
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using DapperApp.Factory;
using login.Models;
using login.Controllers;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Http;
namespace login.Controllers
{
public class HomeController : Controller
{
private readonly UserFactory userFactory;
public HomeController(UserFactory user) {
userFactory = user;
}
// GET: /Home/
[HttpGet]
[Route("")]
public IActionResult Index()
{
return View();
}
[HttpPost]
[Route("")]
public IActionResult Register(Home model)
{
if(!ModelState.IsValid)
{
return View("Index", model);
}
PasswordHasher<Home> Hasher = new PasswordHasher<Home>();
model.Password = Hasher.HashPassword(model, model.Password);
userFactory.Add(model);
TempData["message"] = false;
return RedirectToAction("Index");
}
[HttpPost]
[Route("login")]
public IActionResult Login(Home model)
{
if(model.Password == null || model.Email == null){
TempData["login"] = false;
return RedirectToAction("Index");
}
var pass = userFactory.FindByEmail(model);
var Hasher = new PasswordHasher<Home>();
if(pass == null)
{
TempData["login"] = false;
return RedirectToAction("Index");
}
// Pass the user object, the hashed password, and the PasswordToCheck
if(0 != Hasher.VerifyHashedPassword(model, pass.Password, model.Password))
{
TempData["first_name"] = pass.First_Name;
TempData["last_name"] = pass.Last_Name;
TempData["id"] = pass.Id;
HttpContext.Session.SetString("Id", pass.Id.ToString());
ViewBag.Quotes = userFactory.FindAll();
return View();
}
TempData["login"] = false;
return RedirectToAction("Index");
}
[HttpPost]
[Route("addQuote")]
public IActionResult AddQuote(Quotes model)
{
var test = HttpContext.Session.GetString("Id");
if(!ModelState.IsValid)
{
TempData["id"] = test;
model.Users_id = Convert.ToInt32(test.ToString());
var user2 = userFactory.FindById(model.Users_id);
TempData["first_name"] = user2.First_Name;
TempData["last_name"] = user2.Last_Name;
ViewBag.Quotes= userFactory.FindAll();
return View("Login", model);
}
if(test == null){
return RedirectToAction("Index");
}
model.Users_id = Convert.ToInt32(test.ToString());
userFactory.addQuote(model);
var user = userFactory.FindById(model.Users_id);
TempData["id"] = test;
TempData["first_name"] = user.First_Name;
TempData["last_name"] = user.Last_Name;
ViewBag.Quotes = userFactory.FindAll();
return View("Login", model);
}
[HttpGet]
[Route("logout")]
public IActionResult Logout()
{
return RedirectToAction("Index");
}
[HttpGet]
[Route("del/{id}")]
public IActionResult Del(int Id) // This is my delete method
{
userFactory.DeleteByID(Id);
ViewBag.Quotes2= userFactory.FindAll();
var test = HttpContext.Session.GetString("Id");
var user = userFactory.FindById(Convert.ToInt32(test));
TempData["first_name"] = user.First_Name;
TempData["last_name"] = user.Last_Name;
TempData["id"] = test;
return View("Login");
}
}
}
は私のCSHTMLページには、フォームのアクションは、それが最後にアクションを追加しますつまり、相対(いないコンクリート)の参照を使用している
<h1>Hello @TempData["first_name"] @TempData["last_name"]</h1>
@if(TempData["first_name"]!= null)
{
}
@model login.Models.Quotes
<h1>Add Your Quote</h1>
@using(Html.BeginForm("AddQuote","Home"))
{
<p>
<label>Your Quote</label>
@Html.TextAreaFor(d=>d.quotes)
@Html.ValidationMessageFor(d => d.quotes)
</p>
<input type="submit" name="submit" value="Add my quote!"/>
}
<form action="logout" method="get">
<input type="submit" name="submit" value="Log Out"/>
</form>
<div >
@{
if(ViewBag.Quotes != null)
{
foreach(var quotes in ViewBag.Quotes)
{
//If there are any errors for a field...
<p><q>@quotes.quotes</q></p>
<p class="wrapper">[email protected]_Name @quotes.Last_Name at @quotes.Created_At.ToString("hh"):@quotes.Created_At.ToString("mm")
@quotes.Created_At.ToString("tt") @quotes.Created_At.ToString("MMM") @quotes.Created_At.ToString("dd")
@quotes.Created_At.ToString("yyyy")</p>
if(@quotes.Users_id == Convert.ToInt32(TempData["id"].ToString()))
{
<form action="del/@quotes.Id_Quotes" method="get">
<input type="submit" name="submit" value="Delete"/>
</form>
}
}
}
if(ViewBag.Quotes2 != null)
{
foreach(var quotes in ViewBag.Quotes2)
{
//If there are any errors for a field...
<p><q>@quotes.quotes</q></p>
<p class="wrapper">[email protected]_Name @quotes.Last_Name at @quotes.Created_At.ToString("hh"):@quotes.Created_At.ToString("mm")
@quotes.Created_At.ToString("tt") @quotes.Created_At.ToString("MMM") @quotes.Created_At.ToString("dd")
@quotes.Created_At.ToString("yyyy")</p>
if(@quotes.Users_id == Convert.ToInt32(TempData["id"].ToString()))
{
<form action="del/@quotes.Id_Quotes" method="get">
<input type="submit" name="submit" value="Delete"/>
</form>
}
}
}
}
</div>