在.Net MVC 5中使用视图模型面对空值数据

时间:2019-03-25 22:57:50

标签: .net asp.net-mvc

我是Asp.Net MVC 5中的学生开发人员。我正面临一个空值问题。当我尝试为SQL数据库插入表单数据时,ı会在数据库区域的所有列中看到Null值。我没有找到解决方案。我对代码执行哪种错误?应该很容易出错。但是我没有从任何来源找到。您能帮我解决这个问题吗?

型号:

  public partial class Personel
    {
        public int pid { get; set; }
        public string pAd { get; set; }
        public string pSoyad { get; set; }
        public string pTelNo { get; set; }
        public string yonetici { get; set; }
        public Nullable<int> dprFk { get; set; }

        public virtual Departman Departman { get; set; }
    }

视图类:

 public class viewModel
    {
        public IEnumerable<Departman>DepartmanInfo { get; set; }
        public IEnumerable<Personel> PersonelInfo { get; set; }

        public Personel Personel { get; set; }
    }

我的控制器和创建方法:

public ActionResult Create()
        {
            var model = new viewModel()
            {
                DepartmanInfo = db.Departman.ToList(),
                PersonelInfo = db.Personel.ToList()
            };
            return View("Create", model);
        }
        [HttpPost]
        public ActionResult Create([Bind(Include="pid,pAd,pSoyad,pTelNo,yonetici,dprFk")]Personel pModel)
        {
            var personel = new Personel();

            personel.pAd = pModel.pAd;
            personel.pSoyad = pModel.pSoyad;
            personel.pTelNo = pModel.pTelNo;
            personel.yonetici = pModel.yonetici;
            personel.dprFk = pModel.dprFk;
            db.Personel.Add(personel);
            db.SaveChanges();
            return RedirectToAction("Index", "AdminUI");

        }

我的表单html页面:

@model TelefonRehberi.Models.Class.viewModel

@using (Html.BeginForm("Create","Personel",FormMethod.Post))
{

         <div class="form-group">
             @Html.LabelFor(m=>m.Personel.pAd)
             @Html.TextAreaFor(m => m.Personel.pAd, new { @class = "form-control" })
         </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Personel.pSoyad)
            @Html.TextAreaFor(m => m.Personel.pSoyad, new { @class = "form-control" })
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Personel.pTelNo)
            @Html.TextAreaFor(m => m.Personel.pTelNo, new { @class = "form-control" })
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Personel.dprFk)
            @Html.DropDownListFor(m => m.Personel.dprFk, new SelectList(Model.DepartmanInfo, "dprid", "dprAd"), "Departman  Seçiniz", new { @class = "form-control" })
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Personel.yonetici)
            @Html.DropDownListFor(m => m.Personel.yonetici, new SelectList(Model.PersonelInfo, "pid", "pAd"), "Yönetici Seçiniz ", new { @class = "form-control" })
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>

}

0 个答案:

没有答案