.net 为什么发射台不发射?

ogq8wdun  于 2023-03-09  发布在  .NET
关注(0)|答案(1)|浏览(135)

我有剃刀页

<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>

当我发布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");
        }
ttp71kqs

ttp71kqs1#

当我发布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 = "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();
    }
}

型号:

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>

输出:

**注:**如果您想了解更多关于Razor Page Form提交的详细信息,您可以在这里查看我们的官方文件。

相关问题