-1

ここから大きな助けが必要です。私はAsp.net MVC 4アプリケーション開発が初めてです。実際、送信ボタンをクリックした後、ドロップダウンリストで選択した値をデータベースに保存すると問題に直面しました。

デバッグ ポインターを使用して HTTP ポスト オブジェクトの値をチェックしますが、ドロップダウン リストの選択値が含まれていません。常に部門に null 値が表示されます。その問題を解決するには専門家のアドバイスが必要です。まだ私はそれに対する適切な解決策を持っていません。

モデルクラス:

public partial class tblEmployee
{
    public int EmployeeId { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Nullable<System.DateTime> DateOfBirth { get; set; }
    public Nullable<System.DateTime> DateOfJoin { get; set; }

    public string Position { get; set; }
    public string Office { get; set; }
    public string Division { get; set; }

    public Nullable<decimal> Salary { get; set; }
    public virtual tblDivision Divisions { get; set; }
}


public partial class tblDivision
{
    public int value { get; set; }
    public string Division { get; set; }
    public Nullable<int> SelectId { get; set; }
}

コントローラ クラス:

namespace EmpiteHrSystem.Controllers
{
    public class EmployeeController : Controller
    {
        private EmpiteContext db = new EmpiteContext();

        public ActionResult Create()
        { 
            ViewBag.DivisionOptions = new SelectList(db.tblDivisions, "value","Division");

            return View();
        }

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(tblEmployee tblemployee)
        {
            if (ModelState.IsValid)
            {
                try
                { 
                    db.Entry(tblemployee).State = EntityState.Added;

                    db.tblEmployees.Add(tblemployee);
                    db.SaveChanges();
                }
                catch (ArgumentException ae)
                {
                    ModelState.AddModelError("", ae.Message);

                }
                return RedirectToAction("Index");
            }
        }
    }
}

意見:

@model EmpiteHrSystem.Models.tblEmployee
@{  ViewBag.Title = "Create"; Layout = "~/Views/Shared/_Layout.cshtml";}

@Html.EditorFor(model => model.EmployeeId) 

@Html.ValidationMessageFor(model => model.EmployeeId)

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

@*@Html.EditorFor(model => model.Office)*@

@Html.DropDownList("DivisionOptions")

@Html.ValidationMessageFor(model => model.Division)

@Html.ActionLink("Back to List", "Index")
4

1 に答える 1