Wcf不会将项目保存到数据库

时间:2016-11-03 21:50:27

标签: c# asp.net-mvc wcf

我在数据库中创建新项目时遇到问题。我创建了一个应该由MVC客户端使用的Wcf服务。我在我的Wcf服务(BazaEntities)中创建了一个实体数据模型,我定义了一个在EmployeeTable中创建新员工的create方法:

public bool createEmp(Employee employee)
    {
        using (BazaEntities ben = new BazaEntities())
        {
            try
            {
                EmployeeTable etn = new EmployeeTable();
                etn.EmpName = employee.Name;
                etn.EmpLastName= employee.LastName;
                etn.EmpAddress = employee.Address;
                etn.City= employee.City;
                etn.RoleID = employee.RoleID;
                ben.EmployeeTables.Add(etn);
                ben.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

在我的MVC客户端中,我有一个服务控制器:

public class EmpManController : Controller
{
    public ActionResult IndexEmp()
    {
        EmpManServiceClient emsc = new EmpManServiceClient();
        ViewBag.listEmployees = emsc.findAllEmp();
        return View();
    }

    [HttpGet]
    public ActionResult CreateEmp()
    {
        return View("CreateEmp");
    }

    [HttpPost]
    public ActionResult CreateEmp(EmpManViewModel emvm)
    {
        EmpManServiceClient emsc = new EmpManServiceClient();
        emsc.createEmp(emvm.Employee);
        return RedirectToAction("IndexEmp");
    }
}

一个EmpManServiceClient类,它定义了Wcf服务中的所有方法,包括create方法:

public bool createEmp(Employee employee)
    {
        try
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Employee));
            MemoryStream mem = new MemoryStream();
            ser.WriteObject(mem, employee);
            string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
            WebClient webClient = new WebClient();
            webClient.Headers["Content-type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;
            webClient.UploadString(BASE_URL + "createemp", "POST", data);
            return true;
        }
        catch
        {
            return false;
        }
    }

我的索引视图如下所示:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>IndexEmp</title>
</head>
<body>
    <a href="@Url.Action("CreateEmp", "EmpMan")">Add new employee</a>
    <br /><br />
    <table cellpadding="2" cellspacing="2" border="1">
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>LastName</th>
            <th>Address</th>
            <th>City</th>
            <th>RoleID</th>
        </tr>
        @foreach(var employee in ViewBag.listEmployees)
        {
            <tr>
                <td>@employee.Id</td>
                <td>@employee.Name</td>
                <td>@employee.LastName</td>
                <td>@employee.Address</td>
                <td>@employee.City</td>
                <td>@employee.RoleID</td>
            </tr>
        }
    </table>

</body>
</html>

我的创建视图如下所示:

@{
    Layout = null;
}

@model DiplomskiProjektDrugi_Client.ViewModels.EmpManViewModel

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>CreateEmp</title>
</head>
<body>

    @using (Html.BeginForm("CreateEmp", "EmpMan"))
    {
        <table cellpadding="2" cellspacing="2">
            <tr>
                <td>@Html.LabelFor(model => model.Employee.Name)</td>
                <td>@Html.TextBoxFor(model => model.Employee.NAme)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => model.Employee.LastName)</td>
                <td>@Html.TextBoxFor(model => model.Employee.LastName)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => model.Employee.Address)</td>
                <td>@Html.TextBoxFor(model => model.Employee.Address)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => model.Employee.City)</td>
                <td>@Html.TextBoxFor(model => model.Employee.City)</td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => model.Employee.RoleID)</td>
                <td>@Html.TextBoxFor(model => model.Employee.RoleID)</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" value="Save"/></td>
            </tr>
        </table>
    }

</body>
</html>

启动后,我进入索引页面,其中包含数据库中所有员工的列表以及&#34;添加新员工&#34;按钮。按下按钮,我到达一个页面,我可以在其中输入新员工的数据(姓名,姓氏,地址,城市和角色ID)。输入数据并按保存后,它返回索引页面,但我输入的数据未保存。没有例外或错误。我真的不知道为什么会这样。

0 个答案:

没有答案