0

I want to get dropdown list with values from table. I'm have model:

namespace MobileService.Models
{
    public class Mobile
        {
            public int Id { get; set; }

            public string Model { get; set;}

            Public string EMEI { get; set ;}

            public int MasterId { get; set; }

            public List<Master> MasterList { get; set; }
    }

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

        public string Name { get; set; }
    }

}

In controller I creat action for this model:

    public ActionResult Create()
    {
        return View();
    }

In view:

@model MobileService.Models.Mobile
@using (Html.BeginForm()) {

 @Html.EditorFor(model => model.Model)
 @Html.EditorFor(model => model.EMEI )
 // And here i want to display a dropdown list with all available masters

<button type="submit" class="btn btn-primary">Creat</button>
}

But it always tell me that Model.MasterList is null . But why? In model i told to get list from another model (public List MasterList { get; set; }). Why is it null?

5
  • 4
    Show us the controller code where you suppose to load your model.. Commented Nov 24, 2014 at 20:20
  • C# MVC get values from dropdownlist amazing what a Simple GOOGLE SEARCH can Yield Commented Nov 24, 2014 at 20:22
  • 2
    Model.MasterList is null . But why? - what makes you think it wouldn't be? Commented Nov 24, 2014 at 20:25
  • Try this topic. There's an explanation on how to create DropDownList. Commented Nov 25, 2014 at 0:11
  • But it always tell me that Model.MasterList is null Currently, based on your code, you are not returning anything to View: return View() // it's EMPTY. Therefore, your model is null, even though the View expects Mobile. Later, look for SelectList and DropDownList. SelectList can be tricky but once you have understood it, It's easy to create DropDonwList, or even better DropFownListFor Commented Nov 25, 2014 at 0:14

3 Answers 3

0

This code is in VB. In the beginning even i looked for a lot of time this is the simple solution i found.

Convert the model into list of selectListItems

Dim lstEmpTypes As New List(Of SelectListItem)



lstTicketWrTypes.Add(New SelectListItem With {.Text = row("Something"), .Value = row("Something")})

'Then load it into a ViewBag

ViewBag.EmpTypes = lstEmpTypes 

'And in you View

                 @Html.DropDownList("selectWrType", New SelectList(ViewBag.EmpTypes , "Value", "Text"), New With {.class = "span3"})
Sign up to request clarification or add additional context in comments.

Comments

0

When you query for Mobile you need to Include("MasterList"), or turn lazy loading on on your context (not recommended)

Comments

0

The answer for me was to add this to controller:

   public ActionResult Create()
    {    
     ViewBag.MasterId = new SelectList(db.Masters, "Id", "Name");
     return View();
    }

Then in view to show droplist:

 @Html.DropDownList("MasterId", null, htmlAttributes: new { @class = "form-control" })

Thnx ZikO who let me now that list didn't loaded automatical.

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.