I am facing a problem with model binding in my ASP.NET Core MVC application. Currently, the MVC template is the default one. When I write an HTTP POST method, after a user submits feedback, it is not stored in the feedback object: public async Task<IActionResult> Feedback(FeedbackModel feedback)
HomeController
public IActionResult Feedback()
{
return View("Feedback");
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Feedback(FeedbackModel feedback)
{
if (!ModelState.IsValid)
{
// Log or inspect model state errors
foreach (var error in ModelState.Values.SelectMany(v => v.Errors))
{
_logger.LogError($"Error: {error.ErrorMessage}");
}
return View(feedback);
}
var jsonFeedback = JsonConvert.SerializeObject(feedback);
var deserializedFeedback = JsonConvert.DeserializeObject<object>(jsonFeedback, new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.All
});
string filePath = "feedback.txt";
await System.IO.File.WriteAllTextAsync(filePath, jsonFeedback);
return RedirectToAction("Index");
}
View
@model FeedbackModel
@{
ViewData["Title"] = "Feedback";
}
<h2>@ViewData["Title"]</h2>
@if (TempData["Message"] != null)
{
<div class="alert alert-success">
@TempData["Message"]
</div>
}
<form method="post" >
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Feedback" class="control-label"></label>
<textarea asp-for="Feedback" class="form-control"></textarea>
<span asp-validation-for="Feedback" class="text-danger"></span>
</div>
<button type="submit" asp-controller="Home" class="btn btn-primary">Submit</button>
</form>
@section Scripts {
<partial name="_ValidationScriptsPartial" />
}
Model
using System.ComponentModel.DataAnnotations;
namespace SecureStoreApp.Models
{
public class FeedbackModel
{
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
[Required(ErrorMessage = "Email is required")]
[EmailAddress(ErrorMessage = "Invalid email address")]
public string Email { get; set; }
[Required(ErrorMessage = "Feedback is required")]
public string Feedback { get; set; }
}
}
1 Answer 1
Your model FeedbackModel
has prop FeedBack
, and you set binding parameter name is feedback
that cause conflict
Property on type 'FeedbackModel' has the same name as parameter 'feedback'. This may result in incorrect model binding. Consider renaming the parameter or the property to avoid conflicts. If the type 'FeedbackModel' has a custom type converter or custom model binder, you can suppress this message.
You should rename to avoid conflict, I recommend to rename feedback
to model
, and it should worked.
[ValidateAntiForgeryToken]
// Rename feedback to model
public async Task<IActionResult> Feedback(FeedbackModel model)
{
if (!ModelState.IsValid)
Comments
Explore related questions
See similar questions with these tags.
[FromForm]
attribute?public async Task<IActionResult> Feedback([FromForm] FeedbackModel feedback)