-5

I am not able to get the value of @Html.DropDownListFor to a controller. I am new to MVC. Here I am adding the code.

Model

 public IEnumerable<System.Web.Mvc.SelectListItem> CatID { get; set; }

Controller

public ActionResult Motors()
{
 ViewBag.CatID = new SelectList(db1.motors.ToList(), "id", "cat");
 return View();
}

View

//(in view DATA is properly loading from database)

 @Html.DropDownListFor(m => m.CatID, ViewBag.CatID as SelectList, new { @class = "form-control" })

Controller(Back to controller after post)

[HttpPost]

    public ActionResult Motors(MotorInput collection)

         {
            MotorInput obj = new MotorInput
               {
               CatID = collection.CatID
               }; 
              return View(collection);
         }

I am just looking for a way to get the value of @Html.DropDownListFor selected from view to the controller.

Screenshot of Model

Screenshot of Controller after post

amonk
  • 1,769
  • 2
  • 18
  • 27
  • I'm assuming that the `CatID` property is in `MotorInput` class, am I right? – SᴇM Oct 02 '17 at 08:52
  • You cannot bind a ` –  Oct 02 '17 at 08:55
  • Have a look at my post – Reyan Chougle Oct 02 '17 at 08:55
  • @Stephen Muecke so what changes I should do in my Model and controller. Finding this little difficult to understand. – Abhilash Chalissery Oct 02 '17 at 09:19
  • Have a look at the code in [this question and answer](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o). And your editing data so always use a view model - [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Oct 02 '17 at 09:21
  • ViewModel was MotorInput. I managed to solve the issue, Stephen. Just by changing in Model class ( public int CatID { get; set; } ). I have posted my solution down. Thanks for your help :) – Abhilash Chalissery Oct 02 '17 at 10:40

1 Answers1

0

I got the Solution for above issue.

Model(MotorInput.cs)

public int CatID { get; set; }

Controller(UserController.cs) //To load in View page setting to viewBag from database

public ActionResult Motors()
        {
            this.ViewBag.CatID = new SelectList(this.db1.motors, "id", "cat");
            return View();
        }

View(Motors.cshtml)

@model BLL.Models.MotorInput
@{
    ViewBag.Title = "Motors";
    Layout = "~/Views/Shared/_UserLayout.cshtml";
}

    @using (Html.BeginForm())
    {
         @Html.DropDownList("CatID", (SelectList)ViewData["CatID"], "-- Select Category --", new { @class = "form-control" })


     <input type="submit" value="Create">
    }

Controller(Usercontroller.cs) // After post.

[HttpPost]
public ActionResult Motors(MotorInput collection)// MotorInput is the class where CatId is set as int.(Model class)
           {
             MotorInput obj = new MotorInput
                       {
                           CatID = collection.CatID,
                       };
                    return View(collection);
          }

This helps to get the dropdown value to controller from view.

  • You cannot have the same name for the property your binding to and the `SelectList` - refer [Can the ViewBag name be the same as the Model property name in a DropDownList?](http://stackoverflow.com/questions/37161202/will-there-be-any-conflict-if-i-specify-the-viewbag-name-to-be-equal-to-the-mode/37162557#37162557) –  Oct 02 '17 at 11:39