我正在努力开发一个小型工资单项目。我首先使用代码创建了类。
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]创建方法,因为我尝试了几个但不起作用。
答案 0 :(得分:0)
你的帖子方法应该是这样的
[HttpPost]
public ActionResult CreateEmployeeSalary(FormCollection form)
{
//Example of getting the value from the form
decimal EmployeeId= Convert.ToDecimal(form["EmployeeId"].ToString());
}
答案 1 :(得分:0)
试试这个:
[HttpPost]
public ActionResult CreateEmployeeSalary(EmployeeSalary employeeSalary)
{
if (ModelState.IsValid)
{
db.EmployeesSalaries.Add(employeeSalary);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(employeeSalary);
}