0

我在数据库中有 2 个表:tbleducation,tblemployee。无论如何,对于表 tblemployee,我有字段:employeeID,employeeName ....我想将数据插入到具有以下字段的 tbleducation 中:EmployeeID,Duration,.....和对于表 tbleducation 的 EmployeeID,我想做一个下拉列表,将 tblEmployee 中的所有 EmployeeName 列出到下拉列表中。我的代码如下:

看法

<div id="Education">
<%Html.EnableClientValidation(); %>
<% using (Html.BeginForm("Education","EmployeeProfile")){%>
 <% Html.ValidationSummary(true, "Unsuccessfull"); %>
      <table>
        <tr>
          <td>Employee Name</td>
          <td><%= Html.DropDownListFor(x => x.EmployeeName, Model.Employee, "select EmployeeName")%</td>
          <td>Duration</td>
          <td><%: Html.TextBoxFor(m=> m.Duration, new { id="duration",name="duration"})%>
      <%: Html.ValidationMessageFor(m => m.Duration) %></td>
       <tr>
          <td><input type="submit" name="add" id="add" value="Add" /></td>
      </tr>
    </table>
   <%}%>
</div>

模型

public class UserModels
{

    public string EmployeeName { get; set; }
    public int EmployeeCode { get; set; }
    public IEnumerable<SelectListItem> Employee { set; get; }
}

控制器

public ActionResult Education() {

        var query = (from e in context.tblEmployee_Employee
                     select new
                     {
                         empID = e.Code,
                         EmpName = e.NameEng
                     }
                              ).ToList();

        var model = new UserModels();
        var _Emp = query;
        foreach (var item in _Emp)
        {
            model.EmployeeCode = item.empID;
            model.EmployeeName = item.EmpName;
            model.Employee = new SelectList(_Emp, "EmpName", "EmpName");

        }
        return View(model);
    }
   [HttpPost]
    public ActionResult Education(UserModels model, FormCollection edu) {
        tblEmployee_Education education = new tblEmployee_Education();

        education.Duration = edu["Duration"].ToString();
        education.Certificate = edu["Certificate"].ToString();
        education.Country = edu["Country"].ToString();
        education.SchoolName = edu["SchoolName"].ToString();
        education.Major = edu["Major"].ToString();
        education.SubDescript = edu["SubDescript"].ToString();
        string EName = edu["EmployeeName"].ToString();
        return Content(
           string.Format(
               "Selected role for {0} is {1}", model.EmployeeName, model.EmployeeCode
           )
       );
        context.AddTotblEmployee_Education(education);
        context.SaveChanges();
        return View("Personal");
    }

我收到警告消息“检测到无法访问的代码”。我真的不知道如何解决它。请帮助我,

先谢谢了,

4

1 回答 1

0

在您的 POST 操作中,此处:

return Content(
    string.Format(
        "Selected role for {0} is {1}", model.EmployeeName, model.EmployeeCode
    )
);

你已经返回了一个结果,动作执行结束。

所以摆脱它后面的行,它们将永远不会被执行:

context.AddTotblEmployee_Education(education);
context.SaveChanges();
return View("Personal");

如果您希望它们被执行,请删除该return Content(...)行。

于 2012-07-30T15:43:21.517 回答