0

Good day ! I am very new to ASP.NET and tried to research multiple tutorials and would much appreciate this community's feedback.

I am trying to build a cascading dropdownlist from another model. In the course I created a CombinedModel

Model-view

//Combined model
namespace WebApplication2.Models
{
public class CombinedModel
{
    public RegisterViewModel RegisterViewModel { get; set; }
    public DistrictModel Districtmodel { get; set; }
}
}


    //Model 1
    public class RegisterViewModel
    {
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    public string ConfirmPassword { get; set; }

    public virtual List<DistrictModel> DistrictName { get; set; }

 //Model 2
 namespace WebApplication2.Models
 {
public class DistrictModel
 {
    public int DistrictID { get; set; }
    public string DistrictName { get; set; }
 }
 }

Controller-View

    public ActionResult Register()
    {
        ViewBag.DistrictNameList = new SelectList(db.DistrictModel,               "DistrictName", "DistrictName");
        return View();
    }

View

     model WebApplication2.Models.CombinedModel
     using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
       <div class="col-md-10">

     Html.DropDownListFor(m => m.RegisterViewModel.DistrictName, ViewBag.DistrictNameList, new { @class = "form-control" } )
    </div>

And it doesn't seem to work... I tried changing it to Enum and its still wouldn't comply. I would appreciate your feedback on this !

4 Answers 4

1

Change this line

public virtual List<DistrictModel> DistrictName { get; set; }

To this

public virtual string DistrictName { get; set; }

OR

Change your code as below

Model

public class RegisterViewModel
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        public string Email { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        public string ConfirmPassword { get; set; }

        public virtual string DistrictName { get; set; }

        public virtual SelectList DistrictList { get; set; }

    }
    public class DistrictModel
    {
        public int DistrictID { get; set; }
        public string DistrictName { get; set; }
    }

    public class CombinedModel
    {
        public RegisterViewModel RegisterViewModel { get; set; }
        public DistrictModel Districtmodel { get; set; }
    }

Controller

        CombinedModel obj = new CombinedModel();
        obj.RegisterViewModel = new RegisterViewModel();;
        obj.RegisterViewModel.DistrictList = new SelectList(db.DistrictModel, "DistrictName", "DistrictName");;
        return View(obj);

View

@using (Html.BeginForm("Index", "home", FormMethod.Post,
                                      new { @class = "form-horizontal", role = "form" }))
{
    @Html.DropDownListFor(model => model.RegisterViewModel.DistrictName, Model.RegisterViewModel.DistrictList, new { @class = "form-control" })
    <input type="submit" />
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your question does not have any code about any cascading dropdowns! But i see a problem with your code to render the dropdown.

You need to cast the viewBag items to a SelectList beause the DropDownListFor overload expects that, not a dynamic object!

@using (Html.BeginForm("Index", "home", FormMethod.Post,
                                      new { @class = "form-horizontal", role = "form" }))
{
    @Html.DropDownListFor(m => m.RegisterViewModel.DistrictName, 
                 ViewBag.DistrictNameList as SelectList, new { @class = "form-control" })
    <input type="submit" />
}

2 Comments

Hi shyju, thanks indeed for the correction on cascading, the solution didnt work "as SelectList", its showing error when I add it.
what is the error ? the code should work as long as your action method is setting the ViewBag.DistrictNameList as you shown in the question.
0

thanks for all the replies, I thought to post the final workaround and share it with all. The issue was that I did not setup the models properly. I also had to add view bag in Get/Post and changed to dropdownlistfor in view.

Model

 public class AssetRequest
    {
        public int Id { get; set; }

        [DataType(DataType.Date)]
        [Display(Name = "Request date")]
        public DateTime AssetRequestDate { get; set; }



        [Display(Name = "Facility")]
        public int FacilityId { get; set; }
        public virtual Facility Facility { get; set; }

 public class Facility
    {
        public int Id { get; set; }


        [Display(Name="Asset")]
        public string AssetName { get; set; }

        public virtual ICollection<AssetRequest> AssetRequests { get; set; }
    }

public DbSet<AssetRequest> AssetRequestTable { get; set; }
        public DbSet<Asset> AssetTable { get; set; }

        public DbSet<District> DistrictTable { get; set; }

Controller

 // GET: AssetRequests/Create
        public ActionResult Create()
        {

            ViewBag.RequestStatusId = "New request";
            ViewBag.AssetRequestDate = DateTime.Now;
            ViewBag.AssetId = new SelectList(db.AssetTable, "Id", "AssetName");
            ViewBag.DistrictId = new SelectList(db.FacilitytTable, "Id", "DistrictName");
            ViewBag.RequestTypeId = new SelectList(db.RequestTypeTable, "Id", "RequestTypeName");
            return View();
        }


[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "Id,AssetRequestDate,FacilityId,AssetId,RequestTypeId, RequestStatusId")] AssetRequest assetRequest)
        {
            var currentUser = await manager.FindByIdAsync(User.Identity.GetUserId());

            if (ModelState.IsValid)
            {
                assetRequest.User = currentUser;
                assetRequest.AssetRequestDate = DateTime.Now;
                assetRequest.RequestStatusId = 1;
                db.AssetRequestTable.Add(assetRequest);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

Thanks for all the replies !

Comments

0

Ok

the District Model

 public class DistrictModel
 {
    public int Id { get; set; }
    public string DistrictName { get; set; }
 }

the RegisterViewModel

public class RegisterViewModel
{
  [DataType(DataType.Password)]
  [Display(Name = "Confirm password")]
  public string ConfirmPassword { get; set; }

  //Mapping 
  public int DistrictId { get; set; }
  public virtual List<DistrictModel> District { get; set; }
}

At the Controller Level

 public ActionResult Register()
 {
    List<DistrictModel> districtModelList = db.DistrictModel.ToList();
    ViewData["DistrictNameList"] = districtModelList.Select(x => new SelectListItem { Value = x.Id , Text = x.DistrictName  });
    return View();
 }  

At the View

model RegisterViewModel
@using (Html.BeginForm("Index", "home", FormMethod.Post,
                                  new { @class = "form-horizontal", role = "form" }))
{
  @Html.DropDownListFor(model => model.DistrictId, (IEnumerable<SelectListItem>)ViewData["DistrictNameList"], "[Select District]", new { @class = "form-control" })
  <input type="submit" />
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.