50٪ تخفیف روی تمام دوره‌ها!
پایان تخفیف تا:
مشاهده دوره‌ها
0

کلید خارجی یک جدول از خود جدول

سلام

من جدول زیر را ایجاد کرده ام

  public class City
    {

        public int CityId { get; set; }
        public string Name { get; set; }
        public int CityTypeId { get; set; }
        public int? ParentId { get; set; }
        [ForeignKey(nameof(ParentId))]
        public virtual City Parent { get; set; }
    }

که

   public int? ParentId { get; set; }

که id خود جدول می باشد

و صفحه ثبت را هم بصورت زیر ایجاد کرده ام

@model WebApplication2.Models.City

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>City</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <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.CityTypeId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CityTypeId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CityTypeId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ParentId, "ParentId", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                
                @Html.DropDownList("ParentId", null, htmlAttributes: new {@class = "form-control"})
                @Html.ValidationMessageFor(model => model.ParentId, "", 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>

اما مشکلی که دارم

بار اول مثلا می نویسم ایران درست ثبت می شود و ParentId هم null ثبت میشود

اما برای بار دیگر می خواهم یک کشور دیگر ثبت کنم ParentId را ایران نشان می دهد درصورتیکه من می خواهم بتوانم یک کشور دیگر ثبت کنم که ParentId انهم null باشد

باید چکار کنم

باتشکر

پرسیده شده در 1396/08/25 توسط

1 پاسخ

1

شما بهتره از DropDownGroupListFor استفاده کنید

اول کد زیر رو داخل کنترولر بنویسید

public SelectList GetCountrySelectList() {

    var countries = Country.GetCountries();
    return new SelectList(countries.ToArray(),
                        "Code",
                        "Name");

}

public ActionResult IndexDDL() {

    ViewBag.Country = GetCountrySelectList();
    return View();
}

و بعد , ViewBag.Countries رو از هم از action پر کنید .

@Html.DropDownGroupListFor(m => m.location_id, ViewBag.Countries, "-- Select --", new { 
            @data_val = "true",  // for Required Validation
            @data_val_required = "The Name field is required." // for Required Validation
        })
پاسخ در 1396/08/25 توسط

پاسخ شما