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
<code>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");
}
</code>
<code>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");
}
</code>
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
<code> @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" />
}
</code>
<code> @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" />
}
</code>
@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
<code>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; }
}
}
</code>
<code>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; }
}
}
</code>
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; }
}
}
New contributor
Muhammad Munib Nawaz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1