علت و نحوه رفع خطا در فرم ایجاد رکورد که بلافاصله بعد از زدن کلید ایجاد در فرم ثبت و قبل از ورود به خط اول تابع ثبت با این خطا متوقف میشه
متن خطا
[InvalidCastException: Unable to cast object of type 'System.Single' to type 'System.Array'.]
کد ویو @model WebApplication2.Models.Dossier @{ ViewBag.Title = "Create"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.HiddenFor(model => model.ProfileInfoId) @Html.HiddenFor(model => model.Updated) @Html.HiddenFor(model => model.UpdateId) @Html.HiddenFor(model => model.dossier_num) @Html.HiddenFor(model => model.Inserted) @Html.HiddenFor(model => model.IsActive) @Html.HiddenFor(model => model.StartDate) @Html.HiddenFor(model => model.hire_end_date) @Html.HiddenFor(model => model.hire_start_date) @Html.HiddenFor(model => model.barcode) @Html.HiddenFor(model => model.AsnafCode) @Html.HiddenFor(model => model.degree) @Html.HiddenFor(model => model.mmodirdate) @Html.HiddenFor(model => model.modir_num) <div class="form-horizontal"> <h4>Dossier</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.realstate_title, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.realstate_title, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.realstate_title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.work_address, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.work_address, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.work_address, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.provinceId, "provinceId", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.DropDownList("provinceId", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.provinceId, "", new { @class = "text-danger" }) </div> @Html.LabelFor(model => model.CityId, "CityId", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.DropDownList("CityId", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.CityId, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.municipality_part, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.municipality_part, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.municipality_part, "", new { @class = "text-danger" }) </div> @Html.LabelFor(model => model.Region, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.Region, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Region, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.work_phone, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.work_phone, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.work_phone, "", new { @class = "text-danger" }) </div> @Html.LabelFor(model => model.blue_Plaque, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.blue_Plaque, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.blue_Plaque, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Register_Plaque, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.Register_Plaque, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Register_Plaque, "", new { @class = "text-danger" }) </div> @Html.LabelFor(model => model.Postal_code, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.Postal_code, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Postal_code, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Area, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.Area, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Area, "", new { @class = "text-danger" }) </div> @Html.LabelFor(model => model.groundArea, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.groundArea, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.groundArea, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.BalconyArea, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.BalconyArea, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.BalconyArea, "", new { @class = "text-danger" }) </div> @Html.LabelFor(model => model.UndergroundArea, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.UndergroundArea, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.UndergroundArea, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.WCArea, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.WCArea, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.WCArea, "", new { @class = "text-danger" }) </div> @Html.LabelFor(model => model.BossConfirmation, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> <div class="checkbox"> @Html.EditorFor(model => model.BossConfirmation) @Html.ValidationMessageFor(model => model.BossConfirmation, "", new { @class = "text-danger" }) </div> </div> </div> <div class="form-group"> @Html.LabelFor(model => model.OwnerShipStateId, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.DropDownList("OwnerShipStateId", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.OwnerShipStateId, "", new { @class = "text-danger" }) </div> @Html.LabelFor(model => model.LeaseKindId, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.DropDownList("LeaseKindId", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.LeaseKindId, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.EndBuildingId, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.DropDownList("EndBuildingId", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.EndBuildingId, "", new { @class = "text-danger" }) </div> @Html.LabelFor(model => model.latitude, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.latitude, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.latitude, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.longitude, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.longitude, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.longitude, "", new { @class = "text-danger" }) </div> @Html.LabelFor(model => model.tax_zone, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.EditorFor(model => model.tax_zone, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.tax_zone, "", new { @class = "text-danger" }) </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>
کد تابع ثبت // GET: Dossiers/Create public ActionResult Create() { ViewBag.provinceId = new SelectList(db.provinces, "provinceId", "provinceTitle"); ViewBag.CityId = new SelectList(db.Citys, "CityId", "CityTitle"); ViewBag.EndBuildingId = new SelectList(db.EndBuildings, "EndBuildingId", "EndBuildingTitle"); ViewBag.LeaseKindId = new SelectList(db.LeaseKinds, "LeaseKindId", "LeaseKindTitle"); ViewBag.OwnerShipStateId = new SelectList(db.OwnerShipStates, "OwnerShipStateId", "OwnerShipStateTitle"); return View(); } // POST: Dossiers/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Dossier dossier) { if (ModelState.IsValid) { dossier.AsnafCode = "0"; dossier.barcode = "0"; dossier.IsActive = false; dossier.mmodirdate = DateTime.Now; dossier.ProfileInfoId = 1000; dossier.Updated = DateTime.Now; dossier.UpdateId = 0; dossier.StartDate = DateTime.Now; dossier.dossier_num = "0"; dossier.Inserted= DateTime.Now; dossier.hire_end_date = DateTime.Now; dossier.hire_start_date= DateTime.Now; dossier.degree = "نامشخص"; dossier.modir_num = "0"; db.Dossiers.Add(dossier); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.provinceId = new SelectList(db.provinces, "provinceId", "provinceTitle", dossier.provinceId); ViewBag.CityId = new SelectList(db.Citys, "CityId", "CityTitle", dossier.CityId); ViewBag.EndBuildingId = new SelectList(db.EndBuildings, "EndBuildingId", "EndBuildingTitle", dossier.EndBuildingId); ViewBag.LeaseKindId = new SelectList(db.LeaseKinds, "LeaseKindId", "LeaseKindTitle", dossier.LeaseKindId); ViewBag.OwnerShipStateId = new SelectList(db.OwnerShipStates, "OwnerShipStateId", "OwnerShipStateTitle", dossier.OwnerShipStateId); return View(dossier); }
مکان دقیق ارور رو مشخص کنین
Object مربوط به dossier رو قرار بدین
مشکل به وجود آمده برای عملیات Boxing هستش که امکان تبدیلش ممکن نیست
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Web; namespace DataLayer { public class Dossier { [Key] public int DossierId { get; set; } [Display(Name = "شناسه شخص")] [Required(ErrorMessage = "لطفا {0} را وارد کنید")] public long ProfileInfoId { get; set; } [Display(Name = "شماره پرونده")] [MaxLength(8, ErrorMessage = "{0} نمیتواند بیشتر از {1} باشد")] public string dossier_num { get; set; } = "0"; [Display(Name = "عنوان صنفی ")] [Required(ErrorMessage = "لطفا {0} را وارد کنید")] [MaxLength(64, ErrorMessage = "{0} نمیتواند بیشتر از {1} باشد")] public string realstate_title { get; set; } [Display(Name = "شناسه صنفی ")] [MaxLength(10, ErrorMessage = "{0} نمیتواند بیشتر از {1} باشد")] [MinLength(10, ErrorMessage = "{0} نمیتواند بیشتر از {1} باشد")] public string barcode { get; set; } = "0000000000"; [Display(Name = "کد ایرانیان اصناف ")] [MaxLength(10, ErrorMessage = "{0} نمیتواند بیشتر از {1} باشد")] public string AsnafCode { get; set; }="0"; [Display(Name = "آدرس محل کار ")] [Required(ErrorMessage = "لطفا {0} را وارد کنید")] [MaxLength(256, ErrorMessage = "{0} نمیتواند بیشتر از {1} باشد")] public string work_address { get; set; } [Display(Name = "استان")] [Required(ErrorMessage = "لطفا {0} را وارد کنید")] public int provinceId { get; set; } = 0; [Display(Name = "شهر ")] [Required(ErrorMessage = "لطفا {0} را وارد کنید")] public int CityId { get; set; } = 0; [Display(Name = "منطقه ")] [Required(ErrorMessage = "لطفا {0} را وارد کنید")] public int municipality_part { get; set; } = 0; [Display(Name = "ناحیه ")] [Required(ErrorMessage = "لطفا {0} را وارد کنید")] public int Region { get; set; } = 0; [Display(Name = "تلفن واحد صنفی ")] [Required(ErrorMessage = "لطفا {0} را وارد کنید")] [MaxLength(11, ErrorMessage = "{0} نمیتواند بیشتر از {1} باشد")] public string work_phone { get; set; } [Display(Name = "درجه")] [MaxLength(16, ErrorMessage = "{0} نمیتواند بیشتر از {1} باشد")] public string degree { get; set; } = "نامشخص"; [Display(Name = " پلاک آبی ")] public int blue_Plaque { get; set; } = 0; [Display(Name = " پلاک ثبتی ")] [MaxLength(20, ErrorMessage = "{0} نمیتواند بیشتر از {1} باشد")] public string Register_Plaque { get; set; } [Display(Name = "کدپستی")] [Required(ErrorMessage = "لطفا {0} را وارد کنید")] [MaxLength(10, ErrorMessage = "{0} نمیتواند بیشتر از {1} باشد")] [MinLength(10, ErrorMessage = "{0} نمیتواند بیشتر از {1} باشد")] public string Postal_code { get; set; } [Display(Name = "مساحت")] [MinLength(12, ErrorMessage = "{0} نمیتواند بیشتر از {1} باشد")] public float Area { get; set; } = 0; [Display(Name = " مساحت کف")] public float groundArea { get; set; } = 0; [Display(Name = " مساحت بالکن")] public float BalconyArea { get; set; } = 0; [Display(Name = " مساحت زیرزمین")] public float UndergroundArea { get; set; } = 0; [Display(Name = " مساحت سرویس بهداشتی")] public float WCArea { get; set; } = 0; [Display(Name = "موافقت هیأت مدیره درمورد مساحت")] public bool BossConfirmation { get; set; } [Display(Name = "وضعیت مالکیت ")] public int OwnerShipStateId { get; set; } [Display(Name = " نوع اجاره نامه ")] public int LeaseKindId { get; set; } [Display(Name = " تاریخ شروع اجاره نامه ")] public Nullable<DateTime> hire_start_date { get; set; } [Display(Name = " تاریخ پایان اجاره نامه ")] public Nullable<DateTime> hire_end_date { get; set; } [Display(Name = " نوع پایان کار ")] public int EndBuildingId { get; set; } [Display(Name = "وضعیت")] public bool IsActive { get; set; } [Display(Name = "کاربر")] public int UserID { get; set; } [Display(Name = "تاریخ ثبت")] public DateTime Inserted { get; set; } [Display(Name = "کاربر ویرایش")] public Nullable<int> UpdateId { get; set; } [Display(Name = "تاریخ ویرایش")] public Nullable<DateTime> Updated { get; set; } [Display(Name = "تاریخ تشکیل")] [DisplayFormat(DataFormatString = "{yyyy/MM/dd}", ApplyFormatInEditMode = true)] public Nullable<DateTime> StartDate { get; set; } [Display(Name = "طول جغرافیایی")] public string latitude { get; set; } [Display(Name = "عرض جغرافیایی")] public string longitude { get; set; } [Display(Name = "ناحیه مالیاتی")] public string tax_zone { get; set; } [Display(Name = "تاریخ ثبت نظر هیئت مدیره")] [DisplayFormat(DataFormatString = "{yyyy/MM/dd}", ApplyFormatInEditMode = true)] public Nullable<DateTime> mmodirdate { get; set; } [Display(Name = " شماره نامه نظر هیئت مدیره ")] [MaxLength(10, ErrorMessage = "{0} نمیتواند بیشتر از {1} باشد")] public string modir_num { get; set; } public virtual ProfileInfo ProfileInfo { get; set; } public virtual OwnerShipState OwnerShipState { get; set; } public virtual EndBuilding EndBuilding { get; set; } public virtual User DUser { get; set; } public virtual City DCity { get; set; } public virtual LeaseKind LeaseKind { get; set; } public virtual List<Level> Levels { get; set; } public virtual List<DocumentsUpload> DocumentsUploads { get; set; } public virtual List<license> licenses { get; set; } public Dossier() { } } }
اصلا تو ارور مشخص نمیکنه تو کدوم خط هست برای همین گیج شدم که کدوم داده رو میخواد تبدیل کنه که نمیتونه بعد اصلا آرایه ندارم تو داده هام
یه پیشنهاد:
وقتی تو HttpPost مربوط به Create از Bind استفاده نکردی و قبل از ذخیره اطلاعات اولیه رو تکمیل کردی بنظرم این خط های HiddenFor نیاز نیست بنویسی
@Html.HiddenFor(model => model.ProfileInfoId)
اینا لزومی نداره بنویسی!!!!!
در مورد خطا! elmah رو نصب کن بعد از بروز خطا خط مربوط به خطا رو مشخص بهت نشون میده!!!
البته یه موضوعی هم هست اینکه viewbag های داخل HttpPost رو حذف کن چون نیازی نیست به این خطوط
و سوال اصلی من از شما :
DossierId رو کجا generate میکنید؟ آیا بصورت اتوماتیک توسط sql ایجاد میشه؟ یا نه
اگه Identity Specification رو بصورت اتوماتیک در نظر گرفتید که هیچ اگر نه قبل از saveChange باید Dossierid رو ایجاد کنید!!!! بنظرم type مربوط به dossieid را string بگیرید و بصورت Guid مقدار دهی کنید قبل از ذخیره اطلاعات در DB
doossier.dossierID= Guid.NewGuid().ToString();
برای اینکه ما یه Property رو Null پذیر کنیم ، اگر که ValueType باشه از علامت سوال ( ؟ ) جلوی Type اون استفاده میکنیم
در مدل خودتون به جای
public Nullable<DateTime> mmodirdate { get; set; }
از
public DateTime? mmodirdate { get; set; }
استفاده کنین
بقیه موارد رو هم اصلاح کنین و تست کنین
نتیجه رو اعلام کنین
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)