-2

I'm trying to develop an application using ASP.Net MVC 5 & Entity Framework 6. I'm getting confused with Html helpers.

I have a Driver,Vehicle Entities. When i entering a new driver, there is a select box to select and assign a vehicle to the new driver.

All vehicle RegNo properties should be loaded to the @Html.DropDownListFor() when page loading. And when submitting the page, the ID of the selected vehicle should be inserted to the VehicleID column in Driver entity. This is the UI of Driver view.

enter image description here

This is my Entity

enter image description here

user select the RegNo of the Vehicle in @Html.DropDownListFor() and the ID of the vehicleshoud be submitted to the Entity when submitting the form.

How to do this in runtime?

Thanks in advance.

Chamith
  • 129
  • 1
  • 13
  • `"I have loaded options to the select element using JQuery."` - I thought you were asking how to load the options into the `select`? Can you clarify where you're stuck? – David Jun 28 '16 at 08:19
  • Why would you want to not use the `@Html.DropDownListFor()` method? You losing all the benefits of model binding and validation if you don't –  Jun 28 '16 at 08:20
  • No. I have already loaded options to the select using JQuery. no problem with that. But when submitting the form, the element of the model should be bound with the html element. for example this is a textbox in Html helpers. `@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", @id = "txtFullName"} }) ` this input element has been bounded with the Name element of this Entity/Model. I want to know how to make this in html select created in pure html. not using @Html helpers. – Chamith Jun 28 '16 at 08:24
  • I believe the main benefit of using the helpers is automatic validation attributes (as defined on your models) – haim770 Jun 28 '16 at 08:28
  • @StephenMuecke , At the beginning I used `@Html.DropDownListFor()` and I faced trouble when fetching items from the controller to the DropDownListFor() That is why I decided to use pure html. Can you please instruct me how to load initial items to the @Html.DropDownListFor() ? – Chamith Jun 28 '16 at 08:29
  • `@Html.DropDownListFor(m => m.SomeProperty, Model.Options)` where `Options` is a property in you model that is typeof `IEnumerable` and `SomeProperty` is the property you want to bind to. What problems were you having? –  Jun 28 '16 at 08:31
  • @StephenMuecke this is my case. My view is called `Driver` When I insert a new driver, I have to select & assign a Vehicle for the driver. When this `Driver` view is loading, All the RegNo (a property) should be loaded to the select as options grammatically. And also when submitting the form, Selected vehicle ID should be passed to the VehicleID property in Driver view. ![This is my driver model](http://imgur.com/wQmFQaF). – Chamith Jun 28 '16 at 08:42
  • There is not nearly enough information in you last comment. Edit you question to show the relevant details. –  Jun 28 '16 at 08:44
  • @StephenMuecke, The question has been updated. – Chamith Jun 28 '16 at 09:21
  • Its not clear what problems you would have has using `DropDownListFor()` - refer [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) for a typical usage. If on the other hand, those 2 dropdownlists are cascading, then refer [this DotNetFiddle](https://dotnetfiddle.net/1bPZym) –  Jun 28 '16 at 10:49
  • @Chamith: The question seems to have *significantly* changed since I posted an answer. At this point what is even really being asked? It's not clear what the problem is. – David Jun 28 '16 at 12:11

1 Answers1

1

You can always loop over your collection manually:

<select name="Something">
    @foreach(var thing in Model.Something)
    {
        var selectedStr = someExpression ? "selected" : "";

        <option @selectedStr value="@thing.SomeValue">@thing.SomeText</option>
    }
</select>

It may even be worth it to examine the HTML produced by the HTML helper method so you can replicate it more exactly. It wouldn't be far off from this, the helper methods tend to generate pretty simple and straightforward markup.

As long as the resulting form element has the same name/values, there will be no difference between this and the HTML helper when posting the form.

haim770
  • 48,394
  • 7
  • 105
  • 133
David
  • 208,112
  • 36
  • 198
  • 279