如何基于选定的值从基于@ Html.DropDownList的数据库更新@ html.EditorFor

时间:2018-12-07 04:02:00

标签: model-view-controller

我是新手,正在学习MVC。

方案是-我想基于员工ID(@ html.EditorFor)和请假(@ html.DropDownListFor)获得请假余额。我必须在选择休假(@ html.DropDownListFor)的另一个文本框中显示它。

这是我的模特:

public class Employee : BaseEntity
{
    public string EmpId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string FullName { get; set; }
    public char Gender { get; set; }
    public bool IsActive { get; set; }
    public bool IsResigned { get; set; }

    public virtual ICollection<Leave> Leaves { get; set; }
    public virtual ICollection<Overtime> Overtimes { get; set; }
    public virtual ICollection<OfficialBusiness> OfficialBusinesses { get; set; }
    public virtual ICollection<LeaveBalance> LeaveBalances { get; set; }

}

public class LeaveType : BaseEntity
{
    public int Id { get; set; }
    public string Leave { get; set; }
    public string LeaveDesc { get; set; }
}

public class Leave : BaseEntity
{
    [Display(Name = "ID")]
    public int LeaveId { get; set; }

    [Required]
    [Display(Name = "Leave")]
    public string LeaveType { get; set; }

    [Display(Name = "Paid")]
    public bool WithPay { get; set; }

    [Required]
    [Display(Name = "From")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime FromDate { get; set; }

    [Required]
    [Display(Name = "To")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime ToDate { get; set; }

    [Display(Name = "Days")]
    public int Days { get; set; }

    [Required]
    public string Reason { get; set; }

    [Display(Name = "Status")]
    public int LeaveStatus { get; set; }
    public string Remarks { get; set; }


    public virtual Employee Employee { get; set; }

    [Required]
    public string EmpId { get; set; }

}

public class LeaveBalance : BaseEntity
{
    public int Id { get; set; }
    public decimal Balance { get; set; }
    public bool IsIncluded { get; set; }
    public virtual Employee Employee { get; set; }
    public string EmpId { get; set; }
    public string Leave { get; set; }
}

到目前为止,我认为:

<div class="form-group">
        @Html.LabelFor(model => model.Leave.EmpId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Leave.EmpId, new { htmlAttributes = new { @class = "form-control", @id = "Empid" } })
            @Html.ValidationMessageFor(model => model.Leave.EmpId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Leave.LeaveType, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.Leave.LeaveType, new SelectList(Model.LeaveTypes, "Leave", "LeaveDesc"), "Select Leave",
                    new
                    {
                        @class = "form-control",
                        @id = "LeaveTypes"

                    })
            @Html.ValidationMessageFor(model => model.Leave.LeaveType, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.LeaveBalance.Balance, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.LeaveBalance.Balance, new { htmlAttributes = new { @class = "form-control", @id = "Balance", @readonly = "true" } })
        </div>
    </div>

在我的控制器中:

public ActionResult GetLeaveBal(string empid, string leave)
    {
        var viewModelBal = db.LeaveBalance.Where(l => l.EmpId == empid)
                        .Where(l => l.Leave == leave);

        return View(viewModelBal);
    }

所有表都有数据。 而且我无法获得员工特定请假的请假余额。 请帮助。谢谢。

0 个答案:

没有答案