0

I have an DropDownList ,which is showing list of Status,but when i Select an item from DropDownlist and than i Checked HTML Markup i can see there isnt Selected attr and than i researched and find out I need SelectListItem in my Controller and than i tried to implement it in my Controller,but i got some errors :) as usually i implement DropDown in my Razor View (static) , but this time which is first time :) i want implement in my Controller so it becomes dynamic.
Can anyone point me in right direction :)
Thanks in advance :)

Controller:

    //DropDown
    public List<VMRMA.NameStatusDropDown> GetStatusForDropDown()
    {

        List<VMRMA.NameStatusDropDown> result = new List<VMRMA.NameStatusDropDown>();

        var obj = db.RMAStatus.Select(u => u).ToList();


        if (obj != null && obj.Count() > 0)
        {
            foreach (var data in obj)
            {
                VMRMA.NameStatusDropDown model = new VMRMA.NameStatusDropDown();
                model.Status = data.Status;
                model.ID = data.ID;
                result.Add(model);
            }
        }

        return result;

   }

    //Dropdown runs in this Action
    public ActionResult RMA ()
    {
      VMRMA model = new VMRMA();
      model.NameStatusDropDowns = GetStatusForDropDown();

     //RMA query and some an other stuff

     return View(model);

    }

ViewModel:

    public class VMRMA
    {

        public List<NameStatusDropDown> NameStatusDropDowns { get; set; }


        //DropDown
        public class NameStatusDropDown
        {
            public NameStatusDropDown()
            {

            }
            public NameStatusDropDown(int ID, string Status)
            {
                this.ID = ID;
                this.Status = Status;


            }

            public int ID { get; set; }


   public string Status { get; set; }


    }

}

View:

@using ModelNamespace.Models
@model VMRMA

<form>
<div class="form-group">
<label class="form-control-label">Select a status</label>
<br />
<select>

<option>Select</option>
@foreach (var item in Model.NameStatusDropDowns)
{                                        
  <option value="@item.ID">@item.Status</option>
}
</select>

</div>
  <div class="form-group">
  <input type="submit" value="Send data" class="btn btn-primary">                                 
 </div>

</form>

HTML Markup:

<div class="form-group">
<label class="form-control-label">Select a status</label>
<br>
 <select>
<option>Select</option>
<option value="1">Sendt</option>
<option value="2">Under behandling</option>
<option value="3">Blive behandlet</option>
<option value="4">Modtaget</option>
</select>

</div>
7 seconds
  • 133
  • 2
  • 17
  • 1
    why in the world are you creating your html manually (your `` –  Jun 21 '18 at 11:20
  • Hi @StephenMuecke :) :) I tried like this get error and i know i miss something , would you please correct me :) @Html.DropDownListFor(Model.NameStatusDropDowns,IEnumerable) – 7 seconds Jun 21 '18 at 11:36
  • 1
    Read the link I gave you (the code in your comment is not remotely close to being correct) –  Jun 21 '18 at 11:38
  • @StephenMuecke , i read it , it was great ,but it was about some errors :) which i dont get it :) :) :) is there any way maybe show me how can i fix the – 7 seconds Jun 21 '18 at 11:54
  • What errors? I have no idea what code your are using now –  Jun 21 '18 at 11:55
  • @StephenMuecke i think next time i wrote comment you coming and kick my a** :) :D , sorry in my last comment i forget write to (it was great ,but it was about some errors :) which i dont get that error :) ) , currently i use the code i posted in my question – 7 seconds Jun 21 '18 at 11:57
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173554/discussion-between-stephen-muecke-and-7-seconds). –  Jun 21 '18 at 11:58

3 Answers3

2

This two Post helped me out to solve the problem and Thanks to @Stephen Muecke with his good post, Which is wroted Here and Thanks to this post with great explanation, which is wroted Here.
Here is what i did , maybe it helped someone one day :) :

Add To Property to my View Model :

public class VMRMA
{
        public List<SelectListItem> Status { set; get; }

        public int? SelectedStatus { set; get; }   
}

Change my ActionResult to :

public ActionResult RMA (int Id)
    {
        VMRMA model = new VMRMA();

        model.Status = new SelectList(DatabaseNameSpace.RMAStatus, "ID", 
        "Status").ToList();
        //some an other stuff

       return View(model);
    }

and than change my View to :

@Html.DropDownListFor(s => s.SelectedStatus, Model.Status, "- Select -", new { @class = "form-control" }) 
7 seconds
  • 133
  • 2
  • 17
0

Controller:

ViewBag.Statuses= new SelectList(_context.RMAStatus
                .Select(item => new { value = item.Id, text = item.Status}), "value", "text", selectedId);

View:

@Html.DropDownListFor(x => x.StatusId, ViewBag.Statuses as SelectList, "- please select -")
szydzik
  • 981
  • 8
  • 9
0

Create a partial view as this:

    @model MyApp.Models.MyClass


@{
    Layout = null;
}
@*@Html.Partial("ActionMethod", "Controller", new ViewDataDictionary { { "Name", "TestName" } })*@

@Html.DropDownList((String)TempData["Name"], new SelectList( ViewBag.Specialities,"Value","Text"),
    new { @class = "form-control", @multiple="multiple" });

Then in your controller

 List<MyClass> lstSpecialities = 
        ViewBag.Specialities = lstSpecialities; // Now it is available for the view

Last step, load your view using @Html.RenderAction()

Plexis Plexis
  • 302
  • 2
  • 12