3

I want to display names of mobiles beside checkboxes. Hence I have used the below code with EditorForModel but what it renders in just 12 . I don't understand why it displays 12 instead of names of mobiles.

I have used Ado.net entity model for generating the model class from the database table Mobile.so I have a model class named Mobile.cs as

    namespace MvcDropdown.Models
    {
    using System;
    using System.Collections.Generic;

    public partial class Mobile
    {
        public int Model { get; set; }
        public string Type { get; set; }
        public string Cost { get; set; }
        public Nullable<bool> IsSelected { get; set; }
    }
}

And in HomeController, I have a action method as

public ActionResult checkboxfun()
        {
            GadgetsContext gc = new GadgetsContext();
            return View(gc.Mobiles);
        }

Now I have used the Editor Template by naming the view as Mobile as below.

@model MvcDropdown.Models.Mobile

@Html.HiddenFor(m=>m.Model)
@Html.HiddenFor(m=>m.Type)

@Html.EditorFor(m=>m.IsSelected)
@Html.DisplayFor(m=>m.Type)

And i the checkboxfun view I have the following code.

@model IEnumerable<MvcDropdown.Models.Mobile>

@using(Html.BeginForm())
{
@Html.EditorForModel()     
    <br />
    <input type="submit" value="submit" />
}

Now I'm unable to view the data from the database.

Intriguing
  • 125
  • 3
  • 13
  • 2
    Is you editor template named `Mobile.cshtml` and located in the `/Views/Shared/EditorTemplates` folder? –  Feb 06 '17 at 08:27
  • Which error do you get? Your editor template uses `MvcDropdown.Models.Mobile` as its model but view page uses `IEnumerable`. Also if `Mobiles` is a `DbSet` for `Mobile`, you may try returning `IEnumerable` collection: `return View(gc.Mobiles.ToList());` – Tetsuya Yamamoto Feb 06 '17 at 08:46
  • @TetsuyaYamamoto. `EditorForModel()` accepts both a single object and a collection, and `.ToList()` is not necessary –  Feb 06 '17 at 08:52
  • @Tetsuya I'm unable to display the names of mobiles and checkboxes for names rather It just displayes 12 with a submit button – Intriguing Feb 06 '17 at 09:18
  • @StephenMuecke Editor Template is in /Views/Home/EditorTemplates – Intriguing Feb 06 '17 at 09:20
  • Are you sure you do not have a typo in there, and that it is `/Views/Home/EditorTemplates/Mobile.cshtml` - it is not finding your template (and your have 2 items in the collection :) –  Feb 06 '17 at 09:22
  • @StephenMuecke Sorry I missed this point: http://stackoverflow.com/a/15093097/6378815. The view should first look for templates inside directory which same name as controller name, then searching in Shared directory. – Tetsuya Yamamoto Feb 06 '17 at 09:22
  • @StephenMuecke yes I had a space between editor and Templates so it was not working.Thank Stephen. – Intriguing Feb 06 '17 at 09:24

1 Answers1

0

Your generating 1 2 because the method is not finding a matching template, and will default to rendering the vale of the first property of the model (in your case you have 2 items in the collection with Model = 1 and Model = 2.

Your template must be named Mobile.cshtml to match the class name, and be located in either the /Views/Shared/EditorTemplates or /Views/yourControllerName/EditorTemplates (in your case /Views/Home/EditorTemplates) folder