Asp.net core 在razor页面中传递对象的最简单方法
我是Razor的新手,我正在尝试将数据从“Index.html”传递到“quick.html”,但我正在努力使用不同的教程将这些片段组合在一起。这就是我到目前为止所取得的成就。我正在尝试不使用服务就可以做到这一点,但我不确定这是否可行 谢谢 Index.htmlAsp.net core 在razor页面中传递对象的最简单方法,asp.net-core,razor,razor-pages,Asp.net Core,Razor,Razor Pages,我是Razor的新手,我正在尝试将数据从“Index.html”传递到“quick.html”,但我正在努力使用不同的教程将这些片段组合在一起。这就是我到目前为止所取得的成就。我正在尝试不使用服务就可以做到这一点,但我不确定这是否可行 谢谢 Index.html @page @model IndexModel @{ ViewData["Title"] = "Home page"; } <div class="text-center
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<h1 class="display-4">Welcome, @Model.Name</h1>
<p>This is My Quiz App</a>.</p>
<form method="post">
<label>What's your name?</label>
<input type="text" asp-for="@Model.Visitor.Name">
<button type="submit">Send</button>
</form>
</div>
您可以使用
asp页面
tag helper直接将表单从Index.cshtml提交到quick.cs。在quick.cs
public class QuizModel : PageModel
{
[ViewData]
[BindProperty]
public string Name { get; set; }
[BindProperty]
public Visitor Visitor { get; set; }
public void OnGet()
{
}
public void OnPost()
{
Name = Visitor.Name;
}
}
Index.cshtml:
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<h1 class="display-4">Welcome, @Model.Name</h1>
<p>This is My Quiz App.</p>
<form asp-page="Quiz" method="post">
<label>What's your name?</label>
<input type="text" asp-for="Visitor.Name">
<button type="submit">Send</button>
</form>
</div>
@page
@模型索引模型
@{
ViewData[“Title”]=“主页”;
}
欢迎,@Model.Name
这是我的测验应用程序
你叫什么名字?
发送
结果:
Hi@Pablo Aguirre de Souza,如果我的解决方案可行,您能接受它作为答案吗,谢谢!谢谢你的答复,先生。它工作得很好。所以,如果我理解得很好,秘密就在表单中的asp页面标记上?如果我想在测验页面中使用表单,该表单也将在quick.cs中处理,该怎么办。
public class QuizModel : PageModel
{
[ViewData]
[BindProperty]
public string Name { get; set; }
public Visitor Visitor { get; set; }
public void OnGet()
{
Name = ?
}
public class QuizModel : PageModel
{
[ViewData]
[BindProperty]
public string Name { get; set; }
[BindProperty]
public Visitor Visitor { get; set; }
public void OnGet()
{
}
public void OnPost()
{
Name = Visitor.Name;
}
}
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<h1 class="display-4">Welcome, @Model.Name</h1>
<p>This is My Quiz App.</p>
<form asp-page="Quiz" method="post">
<label>What's your name?</label>
<input type="text" asp-for="Visitor.Name">
<button type="submit">Send</button>
</form>
</div>