4

So, I got a DropdownListfor that looks like that in my view:

@Html.DropDownListFor(m => m.ProductCategory, new SelectList(Model.ProductCategories.OrderBy(m => m.PCNumber), "", "Name"), "")

That works like it should. In my next view the user should be able to edit his order. So what I want to do is, if he opens the form all of his data from before should be displayed, for textboxes I got it work with the following code:

 @Html.TextBoxFor(m => m.NameOfProduct, new { @Value = @Model.NameofProduct })

So now my problem is how can I do the same thing that I did with my textboxes (giving them default values from my model) for a DropDownListFor when the value is stored in my model(database)? It should like that if he selected Category 3 before and now wants to edit his order from before, the dropdownlist should show category 3 right away.

Thank you for any help!

RawMVC
  • 123
  • 1
  • 15
  • Check this question: http://stackoverflow.com/questions/23799091/html-dropdownlistfor-how-to-set-default-value. Certainly you can set DDL default value from either controller or view side. – Tetsuya Yamamoto Oct 10 '16 at 08:33
  • 1
    Never set the value attribute in `HtmlHelper` methods. Its just `@Html.TextBoxFor(m => m.NameOfProduct)`. And for the dropdownlist, you just need to set the value of `ProductCategory` in the GET method before you pass the model to the view (e.g. `model.ProductCategory = 3; return View(model);` –  Oct 10 '16 at 08:40
  • Thank you both for that information. If I understand you correctly @StephenMuecke I would set all my values for my textboxes in the get method too? – RawMVC Oct 10 '16 at 08:45
  • 1
    @RawMVC, Yes, and then your form controls will be bound to your model –  Oct 10 '16 at 08:48
  • Okay, I will do that in that way and I will give a quick feedback here when I tried it! :) – RawMVC Oct 10 '16 at 08:49
  • So I tried it, your idea works well for every textbox, the default values are set und in myview I dont set any values any more :) If I try that for my DropDownListFor, nothing happens, I tried with Product Category as int-property and as string, in both cases, nothing happend when I set ProductCategory = 5; or ProductCategory = "Toys" in the GET-Method and returned my model. – RawMVC Oct 10 '16 at 09:48
  • Okay, this works even better than I thought, I was using some values like "test" or different numbers that were not in my selectlist and it did not work out. If I use the values that are in my list it works, so thanks again for this wonderful tip and how easy this is to handle with ASP.NET MVC :) – RawMVC Oct 10 '16 at 11:05

1 Answers1

3

Try this code may be it work in your situation.

@Html.DropDownListFor(m=> m.PCNumber, new SelectList(m.ProductCategories, "PCNumber", "ProductCategory"), "-- Select Category --", new { @class = "form-control" })

here you will get default edit order in your dropdown

   [HttpGet]
        public ActionResult EditOrder(int? id)
        {
            _obj_order_detail = db.order_detail.Find(id);
            if (_obj_order_detail != null)
            {
                _obj_order_detail.ProductCategories = db.category_detail.ToList(); //get category List
                return View(_obj_order_detail);
            }
            else
            {
                return view();
            }

        }

this will return view with order which you want to edit and ProductCategories and dropdown bu default contain ProductCategory which you want to edit

Ankush Guhe
  • 797
  • 4
  • 17