2013-10-27 99 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); 
}