为什么帖子没有发出?

huangapple go评论148阅读模式
英文:

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 = &quot;Test First Name&quot;;
        Employee.Email = &quot;TestEmail@email.com&quot;;
        Employee.Mobile = &quot;0123456789&quot;;
    }
    public void OnPost()
    {
        Employee = new Employee();
        Employee.ID = Convert.ToInt32(Request.Form[&quot;ID&quot;].ToString());
        Employee.FirstName = Request.Form[&quot;FirstName&quot;].ToString();
        Employee.Email = Request.Form[&quot;Email&quot;].ToString();
        Employee.Mobile = Request.Form[&quot;Mobile&quot;].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

&lt;form method=&quot;post&quot;&gt;
    &lt;input type=&quot;text&quot; style=&quot;display:none;&quot; name=&quot;Id&quot;
           value=&quot;@Model.Employee.ID&quot; /&gt;
    &lt;div class=&quot;form-group&quot;&gt;
        &lt;label for=&quot;name&quot;&gt;Name&lt;/label&gt;
        &lt;input type=&quot;text&quot; class=&quot;form-control&quot; value=&quot;@Model.Employee.FirstName&quot;
               name=&quot;FirstName&quot; id=&quot;FirstName&quot; placeholder=&quot;Enter Name&quot;&gt;
    &lt;/div&gt;
    &lt;br /&gt;
    &lt;div class=&quot;form-group&quot;&gt;
        &lt;label for=&quot;email&quot;&gt;Email address&lt;/label&gt;
        &lt;input type=&quot;email&quot; class=&quot;form-control&quot; value=&quot;@Model.Employee.Email&quot;
               name=&quot;email&quot; id=&quot;email&quot; aria-describedby=&quot;emailHelp&quot;
               placeholder=&quot;Enter email&quot;&gt;
    &lt;/div&gt;
    &lt;br /&gt;
    &lt;div class=&quot;form-group&quot;&gt;
        &lt;label for=&quot;email&quot;&gt;Mobile&lt;/label&gt;
        &lt;input type=&quot;text&quot; class=&quot;form-control&quot; value=&quot;@Model.Employee.Mobile&quot;
               name=&quot;mobile&quot; id=&quot;mobile&quot; aria-describedby=&quot;emailHelp&quot;
               placeholder=&quot;Enter email&quot;&gt;
    &lt;/div&gt;
    &lt;br /&gt;

    &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;A&lt;/button&gt;
&lt;/form&gt;

Output:

为什么帖子没有发出?

Note: If you would like to know more details on Razor Page Form submit you could check our official document here.

huangapple
  • 本文由 发表于 2023年3月8日 14:38:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定