绑定的剃须刀页面没有任何回报
Binded razor page gives nothing back
我正在尝试在 razor 页面中制作一个表单,返回用户在表单中填写的信息。出于某种原因,它没有返回任何已填写的内容。我完全不知道为什么,过去两天我一直在尝试调试。有没有人知道如何解决这个问题或我做错了什么?
class 文章(对于这篇 class 我必须使用抽象的文章):
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;
}
}
新闻文章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;
}
}
以及必须填写所有内容的表格(这个相当大):
@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>
填写所有内容并按下按钮后,这是给定的结果:
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
for some reason it gives nothing back that has been filled in.
确保 删除 setter 的私有访问修饰符。对于Asp.Net Core ModelBinder,它会检查属性是否是私有访问setter。
不确定您的 PageModel 是什么,这里有两种获取表单数据的有效方法。
第一种方式:
[BindProperty] //be sure add this...
public NewsArticle ArticleNews { get; set; }
public void OnPost()
{
//....
}
第二种方式:
public NewsArticle ArticleNews { get; set; }
public void OnPost([Bind(Prefix = "ArticleNews")] NewsArticle newsArticle)
{
//....
}
我正在尝试在 razor 页面中制作一个表单,返回用户在表单中填写的信息。出于某种原因,它没有返回任何已填写的内容。我完全不知道为什么,过去两天我一直在尝试调试。有没有人知道如何解决这个问题或我做错了什么?
class 文章(对于这篇 class 我必须使用抽象的文章):
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;
}
}
新闻文章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;
}
}
以及必须填写所有内容的表格(这个相当大):
@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>
填写所有内容并按下按钮后,这是给定的结果:
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
for some reason it gives nothing back that has been filled in.
确保 删除 setter 的私有访问修饰符。对于Asp.Net Core ModelBinder,它会检查属性是否是私有访问setter。
不确定您的 PageModel 是什么,这里有两种获取表单数据的有效方法。
第一种方式:
[BindProperty] //be sure add this...
public NewsArticle ArticleNews { get; set; }
public void OnPost()
{
//....
}
第二种方式:
public NewsArticle ArticleNews { get; set; }
public void OnPost([Bind(Prefix = "ArticleNews")] NewsArticle newsArticle)
{
//....
}