2

I have two model class in MVC3 one for Services which have those properties

public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public int ChildOf { get; set; }

It also have a DB table by Entityframework

Another model is Quata which have those properties

public int ID { get; set; }
public string Sender_Name { get; set; }
public string Description { get; set; }
.....
......
public Services Service_ID { get; set; }

It also have a DB table by Entityframework

I want to create a Razor(C#) view (for Quata) where user can send a quata by fill a html form but where i wanna show a dropdown list with Services ID as dropdown value and Services Name as dropdown text which is also come dynamically from the Services DB table .

My question is how i should create that dynamic dropdown list by @Html.DropDownListFor ? and send the selected data from that dropdown list to a Controller ?

3 Answers 3

2

Try this

Controller:

 public ActionResult Create()
    {
        var Services = new Services();

        Services.Load(); //load services..

        ViewBag.ID = new SelectList(Services.ToList(), "Id", "Name");


        return View();
    }

[HttpPost]
public ActionResult Create(Quata Quata)
    {
        //save the data 
    }

A strong Typed View: (Using Razor)

@model Quata

@using (Html.BeginForm()) {
<fieldset>
    <legend>Quata</legend>

    <div>
        @Html.LabelFor(model => model.Service_ID.ID, "Service")
    </div>
    <div>
        @Html.DropDownList("ID", String.Empty)
    </div>


    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}
Sign up to request clarification or add additional context in comments.

3 Comments

Great! There is an option without using ViewBag, but you need to change your model.
Really ! what is it ? can u give me that idea ?
ViewModel pattern, which allows a Controller to package up all the information needed to generate the appropriate HTML response. See this: msdn.microsoft.com/en-us/…
0

take a look at @Html.DropDownListFor

Comments

0

So say your viewmodel has a list of said Services.

Something that may work for you is the following (you may not need a for loop here, editor is supposed to eliminate that, but I had some weird binding issues).

In your top level view which points at your viewmodel (@model Quata, assuming Quata is your viewmodel) have this code :

@For i = 0 To Model.DropdownListInput.Count - 1
                Dim iterator = i
                @Html.EditorFor(Function(x) x.DropdownListInput(iterator), "EnumInput")
        Next

In your Editor Template (create a subfolder under the view folder this dropdownlist will be in called editor templates and name the template whatever you desire, mine was EnumInput).

In your editor template, which should point at your model for Services (@model Services) have something like the following code (with substitutions for your appropriate variable names):

@<div class="editor-label">
    @Html.LabelFor(Function(v) v.value, Model.DisplayName)
</div>
@<div class="editor-field">
    @Html.DropDownListFor(Function(v) v.value, New SelectList(Model.ParamEnums, "ValueForScript", "EnumValue"), "--Please Select A Value--")
    @Html.ValidationMessageFor(Function(v) v.value)
</div> 

Replace the list with your list and the lambda values with yours (@Html.DropDownListFor(x => x.id, New SelectList(x.ServiceList, "ID", "Name"), "--Please Select A Value--") or something like that.

Note that this code is in VB, but it should provide a rough guide.

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.