locked
dropdownlist in edit Mode of MVC CRUD operations RRS feed

  • Question

  • User351619809 posted

    I am doing a simple CRUD operation in MVC. When I click on the edit button, I can see the text box to edit all the fields, but I want to display one field called Group as a dropdown field. Below is the code for edit mode. If the user clicks one edit button:

                [HttpGet]
                public ActionResult StoreOrEdit(int id = 0)
                {
                    if (id == 0)
                        return View(new bookAssignment_new());
                    else
                    {
                        using (BookAssignmentEntities db = new BookAssignmentEntities())
                        {
                            return View(db.bookAssignment_new.Where(x => x.ID == id).FirstOrDefault<bookAssignment_new>());
        
                        }
                    }
                }
    

    I get a form like so when I click on edit button:

    enter image description here

    In this form, I want the office to be drop down list so I created a method that returns all the offcies like so:

    private IEnumerable<SelectListItem> GetSelectListItems(IEnumerable<OfficeName> elements)
            {
                var selectList = new List<SelectListItem>();
                foreach (var element in elements)
                {
                    selectList.Add(new SelectListItem
                    {
                        Value = element.OfficeID.ToString(),
                        Text = element.Office.ToString()
                    });
                }
                return selectList;
            }
    

    I also modified my model to show the list:

    public partial class bookAssignment_new
        {
            public int ID { get; set; }
            public Nullable<int> Book { get; set; }
            public string Office { get; set; }
            public IEnumerable<SelectListItem> OfficeName { get; set; }
            public string Group { get; set; }
      }
    

    Model for my office is:

    public partial class OfficeName
        {
            
            public int OfficeID { get; set; }
            public string Office { get; set; }
        }
    

    I just need to return this list to my view using method StoreOrEdit(int id = 0) or something else so that I can display the dropdownlist. I don't know how to do that: Below is my view code:

    model BookAssignments.Models.bookAssignment_new
    @{
        ViewBag.Title = "StoreOrEdit";
    }
    
    @using (Html.BeginForm("StoreOrEdit", "BookAssign", FormMethod.Post, new { onsubmit = "return SubmitForm(this)" }))
    {
        @Html.HiddenFor(model => model.ID)
    
        <div class="form-group">
            @Html.LabelFor(model => model.Book, new { @class = "control-label" })
            @Html.EditorFor(model => model.Book, new { htmlAttributes = new { @class = "form-control", style = "Width:50%" } })
            @*@Html.ValidationMessageFor(model => model.Office)*@
        </div>
     
    
    <div class="form-group">
        @Html.LabelFor(model => model.Office, new { @class = "control-label" })
            @Html.EditorFor(model => model.Office, new { htmlAttributes = new { @class = "form-control", style = "Width:50%" } })
      
      
    </div>
    <div class="form-group">
            @Html.LabelFor(model => model.Group, new { @class = "control-label" })
            @Html.EditorFor(model => model.Group, new { htmlAttributes = new { @class = "form-control", style = "Width:50%" } })
    
        </div>
    

    I am learning MVC and this is my first application in MVC.

    aby help will be greatly appreciated.

    Monday, September 21, 2020 8:46 PM

Answers

All replies