英文:
Why post is not firing?
问题
我有一个剃刀页面
<form method="post" >
<input type="text" style="display:none;" name="Id"
value="@Model.Employee.ID" />
<div class="form-group">
<label for="name">姓名</label>
<input type="text" class="form-control" value="@Model.Employee.FirstName"
name="name" id="name" placeholder="输入姓名">
</div>
<br />
<div class="form-group">
<label for="email">电子邮件地址</label>
<input type="email" class="form-control" value="@Model.Employee.Email"
name="email" id="email" aria-describedby="emailHelp"
placeholder="输入电子邮件">
</div>
<br />
<div class="form-group">
<label for="email">手机号码</label>
<input type="text" class="form-control" value="@Model.Employee.Mobile"
name="mobile" id="mobile" aria-describedby="emailHelp"
placeholder="输入手机号码">
</div>
<br />
<button type="submit" class="btn btn-primary">提交</button>
</form>
当我提交时,public void OnPost() 没有触发?
有什么原因。
public void OnPost()
{
var emp = new DTO.Employee();
emp.EmployeeId = Request.Form["Id"].ToString();
emp.FirstName = "";
emp.LastName = "";
emp.MotherName = "";
emp.SecondName = "";
emp.MaterialStatus = "";
emp.Gender = "";
emp.ForeignFirstName = "";
emp.ForeignLastName = "";
emp.ForeignSecondNamee = "";
emp.ForeignThirdNamee = "";
emp.IsDeleted = true;
emp.Nationality = "";
emp.NoBrothersAllowed = false;
emp.ThirdName = "";
emp.Phone = "";
emp.PassportNumber = "";
emp.Mobile = Request.Form["mobile"].ToString();
emp.Email = Request.Form["Email"].ToString();
emp.FirstName = Request.Form["name"].ToString();
//emp.ID = int.Parse(Request.Form["Id"].ToString());
EmployeeService.Update(emp);
Response.Redirect("Index1");
}
英文:
I have razor page
<form method="post" >
<input type="text" style="display:none;" name="Id"
value="@Model.Employee.ID" />
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" value="@Model.Employee.FirstName"
name="name" id="name" placeholder="Enter Name">
</div>
<br />
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" value="@Model.Employee.Email"
name="email" id="email" aria-describedby="emailHelp"
placeholder="Enter email">
</div>
<br />
<div class="form-group">
<label for="email">Mobile</label>
<input type="text" class="form-control" value="@Model.Employee.Mobile"
name="mobile" id="mobile" aria-describedby="emailHelp"
placeholder="Enter email">
</div>
<br />
<button type="submit" class="btn btn-primary">A</button>
</form>
When I post public void OnPost() not firing ?
Any reason.
public void OnPost()
{
var emp = new DTO.Employee();
emp.EmployeeId = Request.Form["Id"].ToString();
emp.FirstName = "";
emp.LastName = "";
emp.MotherName = "";
emp.SecondName = "";
emp.MaterialStatus = "";
emp.Gender = "";
emp.ForeignFirstName = "";
emp.ForeignLastName = "";
emp.ForeignSecondNamee = "";
emp.ForeignThirdNamee = "";
emp.IsDeleted = true;
emp.Nationality = "";
emp.NoBrothersAllowed = false;
emp.ThirdName = "";
emp.Phone = "";
emp.PassportNumber = "";
emp.Mobile = Request.Form["mobile"].ToString();
emp.Email = Request.Form["Email"].ToString();
emp.FirstName = Request.Form["name"].ToString();
//emp.ID = int.Parse(Request.Form["Id"].ToString());
EmployeeService.Update(emp);
Response.Redirect("Index1");
}
答案1
得分: 1
当我提交
public void OnPost()
时为什么不触发?
实际上,它不会触发,因为您已经将表单绑定到强类型模型@Model.Employee.ID
,所以当您发送请求时,它找不到匹配的模型,您将得不到任何内容。您应该在页面上定义您的模型,这样可以解决您的问题。
正确的方式应该如下所示:
cshtml.cs:
public class EmployeeModel : PageModel
{
public Employee? Employee { get; set; }
public void OnGet()
{
Employee = new Employee();
Employee.ID = 1;
Employee.FirstName = "测试名字";
Employee.Email = "测试邮箱@email.com";
Employee.Mobile = "0123456789";
}
public void OnPost()
{
Employee = new Employee();
Employee.ID = Convert.ToInt32(Request.Form["ID"].ToString());
Employee.FirstName = Request.Form["FirstName"].ToString();
Employee.Email = Request.Form["Email"].ToString();
Employee.Mobile = Request.Form["Mobile"].ToString();
}
}
模型:
public class Employee
{
public int ID { get; set; }
public string FirstName { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
}
cshtml:
@page
@model RazorPageDemoApp.Pages.EmployeeModel
<form method="post">
<input type="text" style="display:none;" name="Id"
value="@Model.Employee.ID" />
<div class="form-group">
<label for="name">名字</label>
<input type="text" class="form-control" value="@Model.Employee.FirstName"
name="FirstName" id="FirstName" placeholder="输入名字">
</div>
<br />
<div class="form-group">
<label for="email">邮箱地址</label>
<input type="email" class="form-control" value="@Model.Employee.Email"
name="email" id="email" aria-describedby="emailHelp"
placeholder="输入邮箱">
</div>
<br />
<div class="form-group">
<label for="email">手机</label>
<input type="text" class="form-control" value="@Model.Employee.Mobile"
name="mobile" id="mobile" aria-describedby="emailHelp"
placeholder="输入手机">
</div>
<br />
<button type="submit" class="btn btn-primary">提交</button>
</form>
注意: 如果您想了解更多关于Razor页面表单提交的详细信息,您可以在此查看我们的官方文档。
英文:
> When I post public void OnPost() not firing ?
Actually, it will not fire because, ou have binded the form with strongly type model which is @Model.Employee.ID
so while you are sending request it doesn't found the matched model and you are not getting anything. You should define your model on the page which would resolve your issue.
Correct way should be as following:
cshtml.cs:
public class EmployeeModel : PageModel
{
public Employee? Employee { get; set; }
public void OnGet()
{
Employee = new Employee();
Employee.ID = 1;
Employee.FirstName = "Test First Name";
Employee.Email = "TestEmail@email.com";
Employee.Mobile = "0123456789";
}
public void OnPost()
{
Employee = new Employee();
Employee.ID = Convert.ToInt32(Request.Form["ID"].ToString());
Employee.FirstName = Request.Form["FirstName"].ToString();
Employee.Email = Request.Form["Email"].ToString();
Employee.Mobile = Request.Form["Mobile"].ToString();
}
}
Model:
public class Employee
{
public int ID { get; set; }
public string FirstName { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
}
cshtml:
@page
@model RazorPageDemoApp.Pages.EmployeeModel
<form method="post">
<input type="text" style="display:none;" name="Id"
value="@Model.Employee.ID" />
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" value="@Model.Employee.FirstName"
name="FirstName" id="FirstName" placeholder="Enter Name">
</div>
<br />
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" value="@Model.Employee.Email"
name="email" id="email" aria-describedby="emailHelp"
placeholder="Enter email">
</div>
<br />
<div class="form-group">
<label for="email">Mobile</label>
<input type="text" class="form-control" value="@Model.Employee.Mobile"
name="mobile" id="mobile" aria-describedby="emailHelp"
placeholder="Enter email">
</div>
<br />
<button type="submit" class="btn btn-primary">A</button>
</form>
Output:
Note: If you would like to know more details on Razor Page Form submit you could check our official document here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论