根据用户的选择创建对象

时间:2015-06-22 10:04:44

标签: c# asp.net-mvc entity-framework

你好我这里有一些UML图: enter image description here

所以问题是我想根据用户选择创建一个员工对象: enter image description here

但我不知道如何在控制器中执行此操作。现在控制器看起来像这样:

 [HttpPost]
    [ValidateAntiForgeryToken]
    [Authorize(Roles = "admin")]
    public ActionResult Create([Bind(Include = "Imie,Nazwisko,Pesel,Data_zatrudnienia,Data_zwolnienia,Pensja,Wyksztalcenie,Nazwa_uzytkownika,Uprawnienia")] Pracownik pracownik)
    {
        if (ModelState.IsValid)
        {

            db.Pracownik.Add(pracownik);
            db.SaveChanges();

            return RedirectToAction("Index");
        }


        return View(pracownik);
    }

您在上面看到的代码会创建两种类型的工作者。我想根据用户的选择创建一个。

在视图中,我根据下拉列表的值创建html对象。像这样:

  <script>
function handleChange() {

    var strg = "";
    var text1 = '<div class="form-group"> @Html.LabelFor(model => model.Pracownik_biurowy.Nazwa_uzytkownika, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10">@Html.EditorFor(model => model.Pracownik_biurowy.Nazwa_uzytkownika, new { htmlAttributes = new { @class = "form-control" } })   @Html.ValidationMessageFor(model => model.Pracownik_biurowy, "", new { @class = "text-danger" }) </div> </div>';
    var text2 = ' <div class="form-group">  @Html.LabelFor(model => model.Przewodnik.Uprawnienia, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Przewodnik.Uprawnienia, new { htmlAttributes = new { @class = "form-control" } })</div></div>';

                $("select option:selected").each(function () {
                    strg = strg + this.value;
                });

                if (strg == "biurowy") {
                    $('#typPracownik').html(text1);


                }

                if (strg == "przewodnik") {

                    $('#typPracownik').html(text2);


                }


            }

            $('#pracownicy').change(handleChange);


        </script>

Osoba:

using System;
using System.Collections.Generic;

public partial class Osoba
{
    public int Id_osoby { get; set; }
    public string Imie { get; set; }
    public string Nazwisko { get; set; }
    public string Pesel { get; set; }

    public virtual Klient Klient { get; set; }
    public virtual Pracownik Pracownik { get; set; }
}

}

Pracownik:

 public partial class Pracownik
{
    public int Id_osoby { get; set; }
    public System.DateTime Data_zatrudnienia { get; set; }
    public Nullable<System.DateTime> Data_zwolnienia { get; set; }
    public double Pensja { get; set; }
    public string Wyksztalcenie { get; set; }

    public virtual Osoba Osoba { get; set; }
    public virtual Pracownik_biurowy Pracownik_biurowy { get; set; }
    public virtual Przewodnik Przewodnik { get; set; }
}

Pracownik biurowy:

    using System;
using System.Collections.Generic;

public partial class Pracownik_biurowy
{
    public int Id_osoby { get; set; }

    public string Nazwa_uzytkownika { get; set; }

    public virtual Pracownik Pracownik { get; set; }
}

Przewodnik:

   using System;
using System.Collections.Generic;

public partial class Przewodnik
{
    public Przewodnik()
    {
        this.Wycieczka_fakultatywna = new HashSet<Wycieczka_fakultatywna>();
    }

    public int Id_osoby { get; set; }

    public string Uprawnienia { get; set; }
    public Nullable<int> Id_regionu { get; set; }

    public virtual Pracownik Pracownik { get; set; }
    public virtual ICollection<Wycieczka_fakultatywna> Wycieczka_fakultatywna { get; set; }
    public virtual Region Region { get; set; }
}

0 个答案:

没有答案