ورود

View Full Version : سوال: اشتباه نمایش داده در DropDownList



Iran58
چهارشنبه 08 فروردین 1397, 15:03 عصر
سلام
من کدهای زیر را نوشته ام


// GET: Home/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
ViewBag.SexId = new SelectList(db.Sexes, "SexId", "Name", person.SexId);
return View(person);
}

و

@model DropDwonListEdit.Models.Person


@{
ViewBag.Title = "Edit";
}


<h2>Edit</h2>




@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.PersonId)


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


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


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


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


@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}




وقتی داده داشته باشه جنسیت هیچ مشکلی نیست
اما مشکلم اینه وقتی برای جنسیت داده ای وارد نکرده باشم باز مقدار DropDownList را برابر با مرد نمایش میدهد درصورتیکه باید null باشد
برای حل این مشکل کدم را چگونه باید تغییر بدهم
باسپاس

raha raad
شنبه 11 فروردین 1397, 11:02 صبح
سلام.
در یکی از overloadهای dropdownList یک پارامتری داره برای نمایش مقدار پیش فرض یا اولیه که میتونید اون رو " " تعیین کنید. امتحانش ضرر نداره شاید درست بشه.

EnKamran
شنبه 11 فروردین 1397, 21:45 عصر
والا این كد كه اصلا نباید تو دراپ دان مقداری نمایش بده، شما كجا مقدار ویوبگ رو به دراپ دان دادین؟

raha raad
چهارشنبه 15 فروردین 1397, 12:49 عصر
والا این كد كه اصلا نباید تو دراپ دان مقداری نمایش بده، شما كجا مقدار ویوبگ رو به دراپ دان دادین؟
ببخشید جوابمو اشتباهی برای شما فزستادم!!!!:ناراحت: