gpt4 book ai didi

c# - 在 Razor 和 MVC 中为表创建提交按钮

转载 作者:行者123 更新时间:2023-12-02 00:48:26 25 4
gpt4 key购买 nike

我是 MVC 的新手,我不确定我的问题出在哪里,但很难找到我面临的挑战。我的保存按钮发布了一条记录,但列都是空的,它不会发布我填写的所有条目。我已经为此工作了两天多,现在你是我的希望。

谢谢。

<div >
<div style="margin-left:8px; margin-right:8px;" class="panel panel-primary ">

<div class="panel-heading">

@{
if (Model.Employees.id > 0) {
<h3 class="panel-title">Update Existing Employee: &nbsp; @Model.Employees.FirstName, @Model.Employees.LastName</h3>
} else if (Model.Employees.FirstName == null) {
<h3 class="panel-title">Add New Employee: &nbsp;</h3>
}
}
</div>
<table id="employee_form">
<tbody id="employees">
<tr class="panel-body">
<td class="form-group">
@using (Html.BeginForm("Save", "Employees")) {
<div class=" col-lg-4 ">
@Html.HiddenFor(e => e.Employees.id)
<ol class="row">
<li style="padding-top:25px;"><label for="FirstName" id="FirstName">First Name: </label><input class="form-control" type="text" value="@Model.Employees.FirstName" /></li>
<li ><label for="LastName" >Last Name: </label><input class="form-control" type="text" value="@Model.Employees.LastName" /></li>
<li ><label for="PhoneNumber" id="">Phone Number: </label><input class="form-control" type="text" value="@Model.Employees.PhoneNumber" /></li>
<li ><label for="Email" id="">Email: </label><input class="form-control" type="text" value="@Model.Employees.Email" /></li>
</ol>
</div>

<div class=" col-lg-4" style="">
<ol>
<li style="padding-top:25px;"><label for="providername" id="ProviderName"> Provider Name: </label><input style="float:right;" class="form-control" type="text" value="@Model.Employees.ProviderName" /></li>
<li><label for="OfficeAddress" id="">Address: </label><input class="form-control" type="text" value="@Model.Employees.OfficeAddress" /></li>
<li><label for="City" id="">City: </label><input class="form-control" type="text" value="@Model.Employees.City" /></li>
<li><label for="State" id="">State: </label><input class="form-control" type="text" value="@Model.Employees.State" /></li>
<li><label for="ZipCode" id="">Zip Code: </label><input class="form-control" type="text" value="@Model.Employees.ZipCode" /></li>

</ol>
</div>

<div class="col-lg-3 ">
<ol>
<li style="padding-top:25px;"><label for=" officefax" id="">Office Fax No.: </label><input class="form-control" type="text" value="@Model.Employees.OfficeFax" /></li>
<li><label for="OfficePhoneNumber" id="">Office Ph. No.: </label><input class="form-control" type="text" value="@Model.Employees.OfficePhoneNumber" /></li>
<li><label for="Notes1" >Notes</label><textarea class="form-control" value="@Model.Employees.Notes"></textarea></li>

</ol>
</div>
<div class="col-lg-12 row">
<button style="float:right;" type="submit" class="btn btn-info">Save</button>
</div>

}
<div class="col-lg-4">

</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<hr />

这是我的 Controller 操作:

[HttpPost]
public ActionResult Save(Employees employees) {
if (!ModelState.IsValid) {
var employeeViewModel = new EmployeesViewModel
{
Employees = employees, EmployeeDetails = _Context.EmployeeDetails.ToList()
};
return View("EmployeeForm", employeeViewModel);
}

if (employees.id == 0) {
_Context.Employees.Add(employees);

} else {
var employeeInDb = _Context.Employees.Single(e => e.id == employees.id);
employeeInDb.FirstName = employees.FirstName;
employeeInDb.LastName = employees.LastName;
employeeInDb.Email = employees.Email;
employeeInDb.DoctorsName = employees.DoctorsName;
employeeInDb.ProviderName = employees.ProviderName;
employeeInDb.City = employees.City;
employeeInDb.State = employees.State;
employeeInDb.ZipCode = employees.ZipCode;

employeeInDb.OfficeAddress = employees.OfficeAddress;
employeeInDb.OfficePhoneNumber = employees.OfficePhoneNumber;
employeeInDb.OfficeFax = employees.OfficeFax;
employeeInDb.Notes = employees.Notes;

}
_Context.SaveChanges();
return RedirectToAction("Index", "Employees");
}

最佳答案

您的输入标签没有名称属性,因此它们不会回发。

例如这个标签:

<input class="form-control" type="text" value="@Model.Employees.LastName" />

你应该使用

@Html.TextBoxFor(m=>m.Employees.LastName)

或者如果你想直接使用输入标签,你可以只添加名称

<input class="form-control" type="text" name="@Html.NameFor(m=>m.Employees.LastName)" value="@Model.Employees.LastName" />

关于c# - 在 Razor 和 MVC 中为表创建提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41945397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com