ویو
@model DataLayer.Dossier @{ ViewBag.Title = "EditProfileInfoes"; } @using (Html.BeginForm("Editdossiers", "Users", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>ویرایش پرونده</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.HiddenFor(m => m.DossierId) @Html.HiddenFor(m => m.ProfileInfoId) @Html.HiddenFor(m => m.dossier_num) @Html.HiddenFor(m => m.Inserted) @Html.HiddenFor(m => m.UserID) @Html.HiddenFor(m => m.Updated) @Html.HiddenFor(m => m.UpdateId) <div class="row"> <div class="col-md-12"> <div class="form-horizontal"> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> <input type="text" name="StartDate" id="StartDate" value="@Model.StartDate" /> @Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.dossier_num, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-4"> @Html.EditorFor(model => model.dossier_num, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.dossier_num, "", new { @class = "text-danger" }) </div> @Html.LabelFor(model => model.barcode, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-4"> @Html.EditorFor(model => model.barcode, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.barcode, "", new { @class = "text-danger" }) </div> @Html.LabelFor(model => model.AsnafCode, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-4"> @Html.EditorFor(model => model.AsnafCode, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.AsnafCode, "", new { @class = "text-danger" }) </div> @Html.LabelFor(model => model.degree, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-4"> @Html.EditorFor(model => model.degree, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.degree, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-1"> <div class="checkbox"> @Html.EditorFor(model => model.IsActive) @Html.ValidationMessageFor(model => model.IsActive, "", new { @class = "text-danger" }) </div> </div> @Html.LabelFor(model => model.BossConfirmation, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-1"> <div class="checkbox"> @Html.EditorFor(model => model.BossConfirmation) @Html.ValidationMessageFor(model => model.BossConfirmation, "", new { @class = "text-danger" }) </div> </div> <div class="control-label col-md-2">استان :</div> <div class="col-md-2"> @Html.DropDownList("provinceId", ViewBag.provinceId as SelectList, "استان را انتخاب نمایید", new { @id = "DropDownListCity",@name= "provinceId" }) @*@Html.DropDownList("CityTitle", ViewBag.states as SelectList, "شهر", new { @class = "inputBox", @id = "DropDownListCity" })*@ </div> </div> <div class="form-group"> @Html.LabelFor(model => model.CityId, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.DropDownList("CityId", ViewBag.states as SelectList, "شهر", new { @id = "CityId", @name = "CityId" }) @Html.ValidationMessageFor(model => model.CityId, "", new { @class = "text-danger" }) </div> @*@Html.LabelFor(model => model.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>*@ @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> </div> </div> <div class="form-group"> <div class="col-md-offset-10 col-md-10"> <input type="submit" value="ذخیره" class="btn btn-success" /> </div> </div> </div> } @section Scripts{ <link href="http://cdn.kendostatic.com/2013.2.918/styles/kendo.common.min.css" rel="stylesheet" /> <link href="http://cdn.kendostatic.com/2013.2.918/styles/kendo.default.min.css" rel="stylesheet" /> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="/Content/js/kendo.all.min.js"></script> <script> $(document).ready(function () { // create DatePicker from input HTML element $("#StartDate").kendoDatePicker(); $("#mmodirdate").kendoDatePicker(); $("#hire_end_date").kendoDatePicker(); $("#hire_start_date").kendoDatePicker(); }); </script> <script> $('#provinceId').change( function GetState() { $.ajax({ url: "@Url.Action("GetJsonState", "Users")", dataType: "json", type: "GET", data: { id: $("#provinceId").val() }, error: function () { }, beforeSend: function () { }, success: function (data) { var items = ""; items = "<option value=''>شهر را انتخاب نمایید</option>"; $.each(data, function (i, item) { items += "<option value='" + item.Value + "'>" + item.Text + "</option>"; }); $("#CityId").html(items); } }); } ); </script> }
کد مربوط به انتخاب استان و شهر
public IList<City> GetCity(int id) { return db.Citys.Where(m => m.provinceId == id).ToList(); } public JsonResult GetJsonState(int id) { var stateListt = this.GetCity(Convert.ToInt32(id)); var statesList = stateListt.Select(m => new SelectListItem() { Text = m.CityTitle, Value = m.CityId.ToString() }); return Json(statesList, JsonRequestBehavior.AllowGet); } ////////////////////////// // GET: InspectReqs/Edit/5 public ActionResult Editdossiers(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Dossier dossiers = db.Dossiers.Find(id); if (dossiers == null) { return HttpNotFound(); } ViewBag.provinceId = new SelectList(db.Provinces, "provinceId", "provinceTitle"); ViewBag.CityId = new SelectList(new List<City>(), "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"); ViewBag.UserID = new SelectList(db.ProfileInfoes, "UserID", "dossier_num"); ViewBag.UserID = new SelectList(db.Users, "UserID", "UserName"); return View(dossiers); } public JsonResult GetState(int provinceId) { return Json(db.Citys.Where(s => s.provinceId == provinceId), JsonRequestBehavior.AllowGet); } // POST: InspectReqs/Edit/5 // 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 Editdossiers(Dossier dossier) { if (ModelState.IsValid) { //inspectReq.UserID = db.Users.Single(u => u.UserName == User.Identity.Name).UserID; dossier.Updated = DateTime.Now; dossier.UpdateId= db.Users.Single(u => u.UserName == User.Identity.Name).UserID; db.Entry(dossier).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("/ShowDossier/" + dossier.ProfileInfoId); } ViewBag.provinceId = new SelectList(db.Provinces, "provinceId", "provinceTitle"); 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); ViewBag.UserID = new SelectList(db.ProfileInfoes, "UserID", "dossier_num", dossier.UserID); ViewBag.UserID = new SelectList(db.Users, "UserID", "UserName", dossier.UserID); return View(dossier); }
دوست عزیز :) یه توضیحی اگر بدید ممنون میشم. فقط با گذاشتن کد نمیشه مشکل رو تشخیص داد.
سلام خسته نباشید من میخوام شهر و استانی که انتخاب کردم و قبلا ثبت شده رو مقادیرش در ویو مربوط به ویرایش به طور معمول نمایش بده ولی هرکاری میکنم همه اطلاعات تو فرم دیده میشه غیر از استان و شهر
اگه تو عکس ببینید مثل فرم اولیه ثبت نشون میده که باید انتخاب کنی دوباره درحای که قبلا موقع ثبت انتخاب شده و توبانک ذخیره شده
یعنی کسی نیست اینو بدونه
یک SelectList شامل چهار پارامتر می باشد که اول item است باید لیست به آن بدهید و دومی فیلد مقدار است و سومی text نمایشی آن و سومی مقدار انتخاب شده.
حال شما باید در کدتان پارامتر آخری را مقدار دهی کنید که این اتفاق در اشکن Editdossiers که GET می باشد نمی افتد.
اینا درست اما تو کد chtml چطوری اینو مقدار دهی کنم من این کد رو نوشتم چه تغییری نیاز هست
@Html.LabelFor(model => model.CityId, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-2"> @Html.DropDownList("CityId", ViewBag.states as SelectList, "شهر", new { @id = "CityId", @name = "CityId" }) @Html.ValidationMessageFor(model => model.CityId, "", new { @class = "text-danger" }) </div>
باید خود selectList رو به Html Helper بدید خودش تشخیص میده.
میشه مثالی بنویسید ممنون میشم
Mr.Madaeni will give me a help please ?
شما نیازی به تغییر خاصی در Html Helper ندارید و صرفا خود اون کار رو براتون انجام میده فقط کافیه توی SelectedList بگید که مقدار انتخابی کدام هستش.
<div class="control-label col-md-2">استان :</div> <div class="col-md-2"> @Html.DropDownList("provinceId", ViewBag.provinceId as SelectList, "استان را انتخاب نمایید", new { @id = "DropDownListCity",@name= "provinceId" }) </div> </div>
منظورتون اینه که این قسمت ViewBag.states as SelectList رو تغییر بدم
این قسمت لیست شهر ها رو برای انتخاب میاره . ولی هرچی میخوام selectedvalue تایپ کنم اون کلمه رو error میده
امکانش هست یک سمپل از کدتون درست کنید و داخل گیت هاب قرار بدید؟ یک سمپل که قابل دیباگ کردن باشه.
تاحالا استفاده نکردم من کل کد رو گذاشتم هم ویو رو هم کنترولر رو فقط دیتا بیس مونده که اونم میشه فقط فیلدهای استان و شهر بمونه بقیه پاک بشه
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)