2013-10-27 70 views
0

我正在尝试开发一个小工资项目。我创建了第一个使用代码的类。HTTPPost创建方法

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace HumanResource.Models 
{ 
    public class SalaryBifurcation 
    { 
     [Key] 
     public int EmployeeSalaryTypeID { get; set; } 
     public string EmployeeSalaryTypeName { get; set; } 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Web; 

namespace HumanResource.Models 
{ 
    public class EmployeeSalary 
    { 

     [Key] 
     public int EmployeeSalaryID { get; set; }  

     public int EmployeeId { get; set; } 
     public EmployeeDetail Employees { get; set; } 

     public int EmployeeSalaryTypeID { get; set; } 
     public virtual ICollection<SalaryBifurcation> SalaryBifurcation { get; set; } 

     public double SalaryAmount { get; set;} 
    } 
} 

我要创建其中显示员工记录的下拉列表,并在此框显示与第二类的SalaryAmount财产SalaryBifurcation名单列表的视图。

我的目标不是硬编码SalaryBifurcation项目例如基本工资,房屋租金津贴等但留下用户添加SalaryBifurcation项目。

MY HTTPGET创建是

public ActionResult CreateEmployeeSalary() 
{  
    ViewBag.EmployeeId = new SelectList(db.EmployeesSalaries, "EmployeeId", "FullName"); 
    ViewBag.salarybifurcation = db.SalaryBifurcation.ToList(); 
    return View(); 
} 

我看来

@model HumanResource.Models.EmployeeSalary  

@{ 
    ViewBag.Title = "CreateEmployeeSalary"; 
} 

<h2>CreateEmployeeSalary</h2> 

@using (Html.BeginForm()) { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>Employee Salary</legend> 

     <div class="editor-label"> 
      @Html.Label("employee id") 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("EmployeeId", string.Empty) 
     </div> 


     @foreach (var item in ViewBag.salarybifurcation) 
     { 

      @item.EmployeeSalaryTypeName 
      @Html.EditorFor(Model => Model.SalaryAmount) 
      <br /> 
     } 


     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
}  

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

任何人可以帮助我发展[HTTPPost]此视图中创建方法,我试了,但无法正常工作。

回答

0

您的POST方法应该是这样的

[HttpPost] 
public ActionResult CreateEmployeeSalary(FormCollection form) 
{ 
    //Example of getting the value from the form 
    decimal EmployeeId= Convert.ToDecimal(form["EmployeeId"].ToString()); 

} 
0

试试这个:

[HttpPost] 
public ActionResult CreateEmployeeSalary(EmployeeSalary employeeSalary) 
{ 
    if (ModelState.IsValid) 
    { 
     db.EmployeesSalaries.Add(employeeSalary); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    return View(employeeSalary); 
}