0

I am trying to learn MVC and have been stuck on this for quite some time, all the tutorial online uses strongly typed view but my view isn't strongly typed,

@using (Html.BeginForm("addInventory", "AdminController", FormMethod.Post))
{
    <div class="form-style-2-heading">Add New Inventory</div>
    <label>
        <span>No <span class="required">*</span></span>@Html.TextBox("no", null, new { id = "no", Class = "input-field" })
    </label>
    <label>
        <span>Name <span class="required">*</span></span>@Html.TextBox("name", null, new { id = "name", Class = "input-field" })
    </label>
    <label>
        <span>Primary Type <span class="required">*</span></span>@Html.DropDownList("typeList", ViewBag.typeList as SelectList, new { id = "primarytype", Class = "select-field" })
    </label>
    <label>
        <span>Secondary Type <span class="required">*</span></span>@Html.DropDownList("typeList", ViewBag.typeList as SelectList, new { id = "secondarytype", Class = "select-field"})
    </label>

    <label><span>&nbsp;</span><input type="submit" value="Submit" /></label>
}

So I successfully binded my dropdown list with data from the controller but I can't seem to do it the other way round

Edit:

Model:

public class inventoryModel
{
    public int no { get; set; }
    public string name { get; set; }
    public int primaryType { get; set; }
    public int secondaryType { get; set; }
}

Controller:

private ActionResult addInventory()
{

   return View();
}
LuxuryWaffles
  • 1,518
  • 4
  • 27
  • 50
  • what is your question? – teo van kot Jul 20 '16 at 06:23
  • You not strongly binding anything. Show your model and the signature of the POST method –  Jul 20 '16 at 06:23
  • @teovankot hi, the question is, I want to know how do I get the data of the html controls from the controller side after I click the button – LuxuryWaffles Jul 20 '16 at 06:28
  • 1
    You creating 2 dropdowns with `name="typeList"` and your model does not even contain a property named `typeList`. If you want to bind to yor model, use `@Html.TextBoxFor(m => m.no)` (ditto for `name`) and for the dropdownlists, `@Html.DropDownListFor(m => m.primaryType, Model.PrimaryTypeList)` and add a property `IEnumerable PrimaryTypeList` to your model (ditto for `secondaryType `) –  Jul 20 '16 at 06:28
  • @StephenMuecke Ohh the dropdownlist is already binded with data from my database, I need to know how to get the selectedValue etc – LuxuryWaffles Jul 20 '16 at 06:33
  • Do it as per my previous comment. And the POST method will be `private ActionResult addInventory(inventoryModel model)` and the model will be correctly bound. –  Jul 20 '16 at 06:37
  • You might also want to look at this [question/answer](http://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) to understand how to create the view model you should be using (do not use data model in a view when editing) –  Jul 20 '16 at 06:38
  • alright thanks, let me digest all these information for a little bit – LuxuryWaffles Jul 20 '16 at 06:42
  • @StephenMuecke to use @Html.TextBoxFor(m => m.no) must the view be strongly binded? – LuxuryWaffles Jul 20 '16 at 06:46
  • Sorry, I don't understand your comment. Using `@Html.TextBoxFor(m => m.no) ` means you strongly type your view to your model. –  Jul 20 '16 at 06:51

1 Answers1

0

Your error is here:

<label>
    <span>Primary Type <span class="required">*</span></span>
    @Html.DropDownList("primaryType", (IEnumerable<SelectListItem>)ViewBag.typeList, new { @class = "select-field" })
</label>
<label>
    <span>Secondary Type <span class="required">*</span></span>
    @Html.DropDownList("secondaryType", (IEnumerable<SelectListItem>)ViewBag.typeList, new { @class = "select-field"})
</label>

Don't use id in htmlAttributes 1st helper parameter is used for tag id and name on POST value of tag will bind to your model property by name.

Also note that I escape class name with @ symbol.

On POST here what your controller signature should be:

[HttpPost]
private ActionResult addInventory(inventoryModel model)
{
   var data = model.secondaryType; //here you can get your posted data
   return View();
}
teo van kot
  • 12,350
  • 10
  • 38
  • 70
  • I got this error: The ViewData item that has the key 'primaryType' is of type 'System.Int32' but must be of type 'IEnumerable'. when I tried it, is it passing the selectlist instead of the selected value? – LuxuryWaffles Jul 20 '16 at 07:27
  • it's easier to use `IEnumerable` instead of `SelectList` just change your typeList to `IEnumerable` i update answer – teo van kot Jul 20 '16 at 07:30
  • 2
    @BloopieBloops - Read the link I gave you in the question - it means that `ViewBag.typeList` is `null` –  Jul 20 '16 at 07:34