在Razor&中创建表的提交按钮; MVC

时间:2017-01-30 21:05:46

标签: c# html asp.net-mvc razor asp.net-mvc-5

我是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 />

这是我的控制器动作:

[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"); 
}

2 个答案:

答案 0 :(得分:3)

您的输入标记没有name属性,因此不会回发。

例如这个标签:

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

答案 1 :(得分:0)

你做错了,这是一个我可以给你的简单例子:

假设您有一个名为Employee的模型:

public class Employee
{
    public int Id {get;set;}
    public string FirstName {get;set;}
    public string LastName {get;set;}

    public Employee() {}
}

你有这样的观点:

@model Employee

@using (Html.BeginForm("Save", "Employees")) 
{
    @Html.AntiForgeryToken()

    @Html.LabelFor(m => m.FirstName)
    @Html.TextBoxFor(m => m.FirstName)

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

    <button type="submit">Save</button>
}

您应该能够在控制器中执行get和post操作,如下所示:

[HttpGet]
public ActionResult Save()
{
    var employee = new Employee();

    return View(employee);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(Empoyee employee)
{
    if (!ModelState.IsValid)
    {
        //do whatever you want here
    }

    return View(employee);
}

我希望这会有所帮助。