Bind Dropdown using Primarykey and Foreignkey relationship of Model In MVC C#

Updated on     Kisan Patel

In previous Article i have give how to create Primarykey and Foreignkey relationship in MVC C# Your Link is HERE

So after creating relationship you just have to create views to bind Dropdown .

so write this code in controller

[HttpGet]
  public ActionResult CreateParent()
  {
      return View();
  }
  [HttpPost]
  public ActionResult CreateParent(ParentCategory model)
  {
      if (ModelState.IsValid)
      {
          db.Parent.Add(model);
          db.SaveChanges();
      }
      return View();
  }

so now rightClick on Createchild method and click on Add View then select Your Model and choose create Scaffold Template and Add view for your Parent View
it will create a view like

@model TestProject1.Models.ParentCategory
@{
 ViewBag.Title = "CreateParent";
 Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>CreateParent</h2>

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

     <fieldset>
         <legend>ParentCategory</legend>

         <div class="editor-label">
             @Html.LabelFor(model => model.ParentName)
         </div>
         <div class="editor-field">
             @Html.EditorFor(model => model.ParentName)
             @Html.ValidationMessageFor(model => model.ParentName)
         </div>

         <p>
              <input type="submit" value="Create" />
         </p>
     </fieldset>
}
<div>
 @Html.ActionLink("Back to List", "Index")
</div>

then Create Createchild method controller to add child as subcategory

Write This Code in controller

 [HttpGet]
 public ActionResult CreateChild()
 {
      ViewBag.ParentId = new SelectList(db.Parent, "Id", "ParentName");
      return View();
 }
 [HttpPost]
 public ActionResult CreateChild(ChildCategpory model)
 {
      if (ModelState.IsValid)
      {
          db.Child.Add(model);
          db.SaveChanges();
      }
      return View();
 }

Here ViewBag name shold be like your Model property name sho it will automatically bind to your dropdownlist

now again rightclick on method and add view of your Createchild method
after that justmodify your view like this

demo
@model TestProject1.Models.ChildCategpory
@{
 ViewBag.Title = "CreateChild";
 Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>CreateChild</h2>
@using (Html.BeginForm())
{
     @Html.AntiForgeryToken()
     @Html.ValidationSummary(true)

     <fieldset>
         <legend>ChildCategpory</legend>

         <div class="editor-label">
              @Html.LabelFor(model => model.ChildName)
         </div>
         <div class="editor-field">
             @Html.EditorFor(model => model.ChildName)
             @Html.ValidationMessageFor(model => model.ChildName)
         </div>

         <div class="editor-label">
             @Html.LabelFor(model => model.ParentId)
         </div>
         <div class="editor-field">
             @Html.DropDownListFor(model => model.ParentId, null, htmlAttributes: new { @class = "form-control" })
             @Html.ValidationMessageFor(model => model.ParentId)
         </div>

         <p>
             <input type="submit" value="Create" />
         </p>
     </fieldset>
}

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

So now run your Project and add some ParentCategory to it. Then go to your ChildCategory page and see your dropdown value

Output of Project

Output of Project


ASP.NET MVC

Leave a Reply