I've pasted below main parts of code that I have problem with. I use ASP.NET Core MVC with Identity. I want to edit user data by passing new ones through Input from Index.cshtml to Process method in OrderController. The problem is that in the Process method, Input.FirstName, Input.SureName etc. are null.
OrderController:
public class OrderController : Controller
{
private readonly ApplicationDbContext _context;
private string strCart = "Cart";
private readonly UserManager<ApplicationUser> _userManager;
public List<Cart> articlesCart = new List<Cart>();
public OrderController(UserManager<ApplicationUser> userManager, ApplicationDbContext context)
{
_context = context;
_userManager = userManager;
}
public ActionResult Index()
{
completeList();
return View();
}
[BindProperty]
public InputModel Input { get; set; } = new InputModel();
public class InputModel
{
[Required]
[Display(Name = "Imię")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Nazwisko")]
public string SureName { get; set; }
[Required]
[Display(Name = "Ulica")]
public string Address { get; set; }
[Required]
[Display(Name = "Miasto")]
public string City { get; set; }
[Required]
[Display(Name = "Kod pocztowy")]
public string PostCode { get; set; }
[Required]
public string PaymentMethod { get; set; }
}
[HttpGet]
public async Task<ActionResult> Process()
{
var user = await _userManager.GetUserAsync(User);
if (user != null)
{
user.FirstName = Input.FirstName;
user.SureName = Input.SureName;
user.Address = Input.Address;
user.City = Input.City;
user.PostCode = Input.PostCode;
await _userManager.UpdateAsync(user);
completeList();
foreach (var cookie in Request.Cookies.Keys)
{
Response.Cookies.Delete(cookie);
}
return View();
}
return NotFound();
}
}
Index.cshtml:
@using Microsoft.AspNetCore.Identity
@using ShopIdentity.Controllers
@using ShopIdentity.Data
@model OrderController
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
<div >
<div style="margin-left: auto; margin-right: auto">
<form id="registerForm" method="post">
<hr />
<div asp-validation-summary="ModelOnly" ></div>
<div >
<input asp-for="Input.FirstName" aria-required="true" />
<label asp-for="Input.FirstName"></label>
<span asp-validation-for="Input.FirstName" ></span>
</div>
<br />
<div >
<input asp-for="Input.SureName" aria-required="true" />
<label asp-for="Input.SureName"></label>
<span asp-validation-for="Input.SureName" ></span>
</div>
<br />
<div >
<input asp-for="Input.Address" aria-required="true" />
<label asp-for="Input.Address"></label>
<span asp-validation-for="Input.Address" ></span>
</div>
<br />
<div >
<input asp-for="Input.City" aria-required="true" />
<label asp-for="Input.City"></label>
<span asp-validation-for="Input.City" ></span>
</div>
<br />
<div >
<input asp-for="Input.PostCode" aria-required="true"/>
<label asp-for="Input.PostCode"></label>
<span asp-validation-for="Input.PostCode" ></span>
</div>
<br />
<div >
<select style="width: 200px" asp-for="Input.PaymentMethod" id="Input.PaymentMethod" class ="dropdown" name="Input.PaymentMethod">
<option selected hidden>Metoda płatności</option>
<option value="Gotówka">Gotówka</option>
<option value="PayPal">PayPal</option>
<option value="Karta płatnicza">Karta płatnicza</option>
<option value="Przelew bankowy">Przelew bankowy</option>
</select>
</div>
<br />
<a asp-action="Proces" id="registerForm" asp-controller="Order" >Złóż zamówienie</a>
</form>
</div>
<div >
</div>
</div>
@section Scripts {
<partial name="_ValidationScriptsPartial" />
}
UPDATE:
Process method:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Proces([Bind("FirstName,SureName,Address,City,PostCode,PaymentMethod")] InputModel Input)
{
the same code
}
Index.cshtml:
@model ShopIdentity.Models.InputModel
...
<div >
<input asp-for="FirstName" aria-required="true" />
<label asp-for="FirstName"></label>
<span asp-validation-for="FirstName" ></span>
</div>
...
CodePudding user response:
Try refactoring your asp-for statements to only use the property name (e.g., change asp-for="Input.PostCode" to asp-for="PostCode"). Your model binding is looking for the property PostCode and not finding it because your request data has Input.PostCode as the property name.
CodePudding user response:
Try to use button rather than <a> tag to submit form:
<form id="registerForm" method="post" asp-action="Proces" asp-controller="Order">
...
<input type="submit" value="Złóż zamówienie"/>
</form>
