مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

aliaaaaa2236

عضویت از 1397/09/21

مشکل در ذخیره اطلاعات و DropDown list

  • جمعه 23 آذر 1397
  • 22:26
تشکر میکنم

سلام من یک جدول Person دارم که اطلاعات شخص رو ذخیره میکنه و دوتا فیلد داره برای اسم شهر و مدرک شخص که کلید خارجی هستند از جدول های City و Madrak :

وقتی میخوام اطلاعات رو ذخیره کنم خطا میده.فکر میکنم مشکل از DropDownList هام باشه چون اون ها رو با ViweModel مقدار دادم :

کدهام به صورت زیر هستند:

کد اکشن ایجاد شخص جدید :

 [HttpGet]
        public ActionResult CreatePerson()
        {
            db.Configuration.ValidateOnSaveEnabled = false;
            var City = db.City.ToList();
            var madrak = db.Madrak.ToList();
            //var model = new VM_AddPerson(City, madrak);

            var model = new VM_AddPerson
            {
                City = City,
                Madrak = madrak
            };
            return View(model);
        }


        [HttpPost]
        public ActionResult CreatePerson(Person per)
        {
            db.Configuration.ValidateOnSaveEnabled = false;
            try
            {
                Person p = new Person();

                p.FirstName = per.FirstName;
                p.LastName = per.LastName;
                p.UserName = per.UserName;
                p.MadrakID = per.MadrakID;
                p.Stutus = false;
                p.PassWord = per.PassWord;
                p.CityID = per.CityID;

                p.Address = per.Address;
                p.Email = per.Email;
                p.Image = per.Image;

                db.Person.Add(p);
                db.SaveChanges();

                return RedirectToAction("ShowPerson", "Home");
            }
            catch
            {
                return View();
            }
           
        }

   }

کد ویو ام به صورت زیر هست :

@model WebApplication1.ViewModels.VM_AddPerson

@{
    ViewBag.Title = "CreatePerson";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>CreatePerson</h2>

@using (Html.BeginForm("CreatePerson","Home",FormMethod.Post)) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Person</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Person.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Person.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Person.FirstName, "", new { @class = "text-danger" })
            </div>
        </div>

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

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

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

        <div class="form-group">
            @Html.LabelFor(model => model.Person.CityID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Person.CityID, new SelectList(Model.City, "CityID", "CityName"),"شهر خود را انتخاب کنید")
                @Html.ValidationMessageFor(model => model.Person.CityID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Person.MadrakID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Person.MadrakID, new SelectList(Model.Madrak, "MadrakID", "Title"),"مدرک خود را انتخاب کنید")
                @Html.ValidationMessageFor(model => model.Person.MadrakID, "", new { @class = "text-danger" })
            </div>
        </div>




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

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

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

        <div class="form-group">
            @Html.LabelFor(model => model.Person.Stutus, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.Person.Stutus)
                    @Html.ValidationMessageFor(model => model.Person.Stutus, "", new { @class = "text-danger" })
                </div>
            </div>
        </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>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

ویومدل رو به صورت زیر ایجاد کردم :

namespace WebApplication1.ViewModels
{
    public class VM_AddPerson
    {
        public Person  Person { get; set; }

        public IEnumerable<City> City { get; set; }

        public IEnumerable<Madrak> Madrak { get; set; }

    }
}

وقتی بریک پوینت میگیرم اون فیلد هایی که تو اکشن پست هستند هیچکدوم مقدار نمیگیرند

و خطالی Object reference not set to an instance of an object. میده

لطفا کامل راهنمایی کنید .ممنون

پاسخ های این پرسش

تعداد پاسخ ها : 1 پاسخ
کاربر سایت

سهیل علیزاده

عضویت از 1396/04/09

  • شنبه 24 آذر 1397
  • 09:28

خطا را به همراه Stack Trace قرار بدید.

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)