I’m completely new to ASP.NET MVC, I’m writing a blog application that consists of articles. As an admin you have a Dashboard where I can add articles:
<code> @model BlogApplication.Models.DashboardViewModel
....
<h3>Add New Article</h3>
@using (Html.BeginForm("AddArticle", "Admin", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(m => m.NewArticle.Title, "Title")
@Html.TextBoxFor(m => m.NewArticle.Title, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.NewArticle.ShortDescription, "Short Description")
@Html.TextBoxFor(m => m.NewArticle.ShortDescription, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.NewArticle.Content, "Content")
@Html.TextAreaFor(m => m.NewArticle.Content, new { @class = "form-control", rows = 5 })
</div>
<div class="form-group">
@Html.LabelFor(m => m.NewArticle.TagIds, "Tags")
@Html.ListBoxFor(m => m.NewArticle.TagIds, new MultiSelectList(Model.Tags, "TagId", "TagName"), new { @class = "form-control" })
</div>
<button type="submit" class="btn btn-primary">Add Article</button>
}
</code>
<code> @model BlogApplication.Models.DashboardViewModel
....
<h3>Add New Article</h3>
@using (Html.BeginForm("AddArticle", "Admin", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(m => m.NewArticle.Title, "Title")
@Html.TextBoxFor(m => m.NewArticle.Title, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.NewArticle.ShortDescription, "Short Description")
@Html.TextBoxFor(m => m.NewArticle.ShortDescription, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.NewArticle.Content, "Content")
@Html.TextAreaFor(m => m.NewArticle.Content, new { @class = "form-control", rows = 5 })
</div>
<div class="form-group">
@Html.LabelFor(m => m.NewArticle.TagIds, "Tags")
@Html.ListBoxFor(m => m.NewArticle.TagIds, new MultiSelectList(Model.Tags, "TagId", "TagName"), new { @class = "form-control" })
</div>
<button type="submit" class="btn btn-primary">Add Article</button>
}
</code>
@model BlogApplication.Models.DashboardViewModel
....
<h3>Add New Article</h3>
@using (Html.BeginForm("AddArticle", "Admin", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(m => m.NewArticle.Title, "Title")
@Html.TextBoxFor(m => m.NewArticle.Title, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.NewArticle.ShortDescription, "Short Description")
@Html.TextBoxFor(m => m.NewArticle.ShortDescription, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.NewArticle.Content, "Content")
@Html.TextAreaFor(m => m.NewArticle.Content, new { @class = "form-control", rows = 5 })
</div>
<div class="form-group">
@Html.LabelFor(m => m.NewArticle.TagIds, "Tags")
@Html.ListBoxFor(m => m.NewArticle.TagIds, new MultiSelectList(Model.Tags, "TagId", "TagName"), new { @class = "form-control" })
</div>
<button type="submit" class="btn btn-primary">Add Article</button>
}
The Action:
<code> [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddArticle(ArticleViewModel model)
{
if (ModelState.IsValid)
{
var article = new Article
{
Title = model.Title,
Content = model.Content,
ShortDescription = model.ShortDescription,
DatePublished = DateTime.Now,
AuthorId = (int)Session["UserId"]
};
_articleService.AddArticle(article, model.TagIds);
return RedirectToAction("Dashboard");
}
// Log validation errors for debugging
var errors = ModelState.Values.SelectMany(v => v.Errors);
foreach (var error in errors)
{
System.Diagnostics.Debug.WriteLine(error.ErrorMessage);
}
var articles = _articleService.GetAllArticles();
var comments = _commentService.GetAllComments();
var tags = _tagService.GetAllTags();
var viewModel = new DashboardViewModel
{
Articles = articles,
Comments = comments,
Tags = tags,
NewArticle = model
};
return View("Dashboard", viewModel);
}
</code>
<code> [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddArticle(ArticleViewModel model)
{
if (ModelState.IsValid)
{
var article = new Article
{
Title = model.Title,
Content = model.Content,
ShortDescription = model.ShortDescription,
DatePublished = DateTime.Now,
AuthorId = (int)Session["UserId"]
};
_articleService.AddArticle(article, model.TagIds);
return RedirectToAction("Dashboard");
}
// Log validation errors for debugging
var errors = ModelState.Values.SelectMany(v => v.Errors);
foreach (var error in errors)
{
System.Diagnostics.Debug.WriteLine(error.ErrorMessage);
}
var articles = _articleService.GetAllArticles();
var comments = _commentService.GetAllComments();
var tags = _tagService.GetAllTags();
var viewModel = new DashboardViewModel
{
Articles = articles,
Comments = comments,
Tags = tags,
NewArticle = model
};
return View("Dashboard", viewModel);
}
</code>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddArticle(ArticleViewModel model)
{
if (ModelState.IsValid)
{
var article = new Article
{
Title = model.Title,
Content = model.Content,
ShortDescription = model.ShortDescription,
DatePublished = DateTime.Now,
AuthorId = (int)Session["UserId"]
};
_articleService.AddArticle(article, model.TagIds);
return RedirectToAction("Dashboard");
}
// Log validation errors for debugging
var errors = ModelState.Values.SelectMany(v => v.Errors);
foreach (var error in errors)
{
System.Diagnostics.Debug.WriteLine(error.ErrorMessage);
}
var articles = _articleService.GetAllArticles();
var comments = _commentService.GetAllComments();
var tags = _tagService.GetAllTags();
var viewModel = new DashboardViewModel
{
Articles = articles,
Comments = comments,
Tags = tags,
NewArticle = model
};
return View("Dashboard", viewModel);
}
The model:
<code> public class ArticleViewModel
{
[Required(ErrorMessage = "The Title field is required.")]
public string Title { get; set; }
[Required(ErrorMessage = "The Content field is required.")]
public string Content { get; set; }
[Required(ErrorMessage = "The Short Description field is required.")]
public string ShortDescription { get; set; }
[Required(ErrorMessage = "The TagIds field is required.")]
public List<int> TagIds { get; set; }
}
</code>
<code> public class ArticleViewModel
{
[Required(ErrorMessage = "The Title field is required.")]
public string Title { get; set; }
[Required(ErrorMessage = "The Content field is required.")]
public string Content { get; set; }
[Required(ErrorMessage = "The Short Description field is required.")]
public string ShortDescription { get; set; }
[Required(ErrorMessage = "The TagIds field is required.")]
public List<int> TagIds { get; set; }
}
</code>
public class ArticleViewModel
{
[Required(ErrorMessage = "The Title field is required.")]
public string Title { get; set; }
[Required(ErrorMessage = "The Content field is required.")]
public string Content { get; set; }
[Required(ErrorMessage = "The Short Description field is required.")]
public string ShortDescription { get; set; }
[Required(ErrorMessage = "The TagIds field is required.")]
public List<int> TagIds { get; set; }
}
ModelState.IsValid
is being set to false,
Although when I check the payload at the network tab on devtools the data exists.
I don’t understand why the validation is not passing. What can I do?
New contributor
Itay Nafrin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.