Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Razor&;中为表创建提交按钮;MVC_C#_Html_Asp.net Mvc_Razor_Asp.net Mvc 5 - Fatal编程技术网

C# 在Razor&;中为表创建提交按钮;MVC

C# 在Razor&;中为表创建提交按钮;MVC,c#,html,asp.net-mvc,razor,asp.net-mvc-5,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">

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

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

例如,此标记:

<input class="form-control" type="text" value="@Model.Employees.LastName" />
或者,如果您想直接使用输入标记,您可以添加名称

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

你做错了,下面是一个简单的例子:

假设您有一个名为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>
}
[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);
}
我希望这有帮助

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