i am trying to make a form in razor page that gives back the information a user has filled in in the forms. for some reason it gives nothing back that has been filled in. I have absolutely no idea why and i've been trying to debug for the last two days. is there someone that maybe knows how to fix this problem or what i did wrong?
the class article (for this class i have to use an abstract one):
public abstract class Article
{
public Article()
{ }
public Article(int id, string title, string text)
{
this.id = id;
this.title = title;
this.text = text;
}
[Required]
public int id { get; private set; }
public string title { get; private set; }
public string text { get; private set; }
public bool HasID(int id)
{
if (id == this.id)
{
return true;
}
return false;
}
public override string ToString()
{
return "Id: " id " Title: " title " Text: " text;
}
}
the news article class:
public class NewsArticle : Article
{
public NewsArticle()
{}
public NewsArticle(int id, string title, string text, bool isEmergency, string byLine, string source)
: base(id, title, text)
{
this.isEmergency = isEmergency;
this.byLine = byLine;
this.source = source;
}
public bool isEmergency { get; private set; }
public string byLine { get; private set; }
public string source { get; private set; }
public override string ToString()
{
return base.ToString() " Made by: " byLine " Used sources: " source;
}
}
and the form where everything has to be filled in (this one is rather big):
@page
@model ModNews.Pages.ModNews_Pages.MakeNewsArticleModel
@{
}
<h1>Create News article</h1>
<div class="form_container_makeNewsArticle">
<form method="post">
<label asp-for="ArticleNews.id">Article number*</label>
<span asp-validation-for="ArticleNews.id"></span><br />
<input asp-for="ArticleNews.id" placeholder="The article id..." />
<label asp-for="ArticleNews.title">Article title*</label>
<span asp-validation-for="ArticleNews.title"></span><br />
<input asp-for="ArticleNews.title" placeholder="The title of the article..." />
<label asp-for="ArticleNews.title">Byline*</label>
<span asp-validation-for="ArticleNews.byLine"></span><br />
<input asp-for="ArticleNews.byLine" placeholder="It is written by..." />
<label asp-for="ArticleNews.text">Article text*</label>
<span asp-validation-for="ArticleNews.text"></span><br />
<input asp-for="ArticleNews.text" placeholder="Your article text..." />
<div class="normalOrEmergency">
<label asp-for="ArticleNews.isEmergency">Is it an emergency:</label>
<select asp-for="ArticleNews.isEmergency">
<option value=""></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
<label asp-for="ArticleNews.source">Article source*</label>
<span asp-validation-for="ArticleNews.source"></span><br />
<input asp-for="ArticleNews.source" placeholder="Your article source..." />
<div class="button_container_makeArticle">
<input type="submit" value="Make Article" />
</div>
</form>
<div><p>@ViewData["Message"]</p></div>
</div>
after filling everything in and pussing the button this is the given result:
The article id is: 0The title is: It is written by: The written text is: It was anFalse news articleAnd the used source was: False
CodePudding user response:
for some reason it gives nothing back that has been filled in.
Be sure remove private access modifier for setter. For Asp.Net Core ModelBinder, it will check whether the property is private access setter.
Not sure what is your PageModel, here are two working ways to get the form data.
The first way:
[BindProperty] //be sure add this...
public NewsArticle ArticleNews { get; set; }
public void OnPost()
{
//....
}
The second way:
public NewsArticle ArticleNews { get; set; }
public void OnPost([Bind(Prefix = "ArticleNews")] NewsArticle newsArticle)
{
//....
}