0

我正在尝试开发一个小型工资单项目。我首先使用代码创建了类。

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;}
    }
}

我想创建一个视图,其中显示了员工记录的下拉列表,并在此框下显示了 SalaryBifurcation 列表的列表以及第二类的 SalaryAmount 属性。

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

我的 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] 创建方法,因为我尝试了几个但没有工作。

4

2 回答 2

0

你的 post 方法应该是这样的

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

}
于 2013-10-27T10:41:29.627 回答
0

尝试这个:

[HttpPost]
public ActionResult CreateEmployeeSalary(EmployeeSalary employeeSalary)
{
    if (ModelState.IsValid)
    {
        db.EmployeesSalaries.Add(employeeSalary);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }
    return View(employeeSalary);
}
于 2013-10-27T10:43:41.277 回答