1

I have a viewmodel as below :

public class WhItemForm
{
    public virtual WhItem WhItem { get; set; }
    public virtual WhItemStock WhItemStock { get; set; }
}

And in my controller, i use ViewBag to fill the select list as below :

public ActionResult Create()
    {
        ViewBag.WhBrandId = new SelectList(db.WhBrand, "Id", "Name");
        ViewBag.WhStockCardId = new SelectList(db.WhStockCard, "Id", "Name");
        ViewBag.WhLocationId = new SelectList(db.WhLocation, "Id", "Name");
        return View();
    }

I have BrandID property in my WHITEM model, here is my WHITEM model .

public class WhItem
{
    public int Id { get; set; }

    public int WhBrandId { get; set; }
    public virtual WhBrand WhBrand { get; set; }

}

When i post the data from view, i am not able to set WhBrandID, my view as below :

 <div class="form-group">
            @Html.LabelFor(model => model.WhItem.WhBrandId, "WhBrandId", new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("WhBrandId", String.Empty)
                @Html.ValidationMessageFor(model => model.WhItem.WhBrandId)
            </div>
        </div>

I know, i need to pass this value as WhItem.WhBrandId since i am using viewmodel in my view. I can do this for @html.TextBoxFor but how can i correct my dropdown list to pass its value as WhItem.WhBrandId to the controller.

Regards

4
  • 1
    Can you not use DropDownListFor? Like so @Html.DropDownListFor(model => model.WhItem.WhBrandId, ViewBag.WhBrandId)? Commented Apr 24, 2014 at 10:16
  • I think yes this should be my answer but, i am getting syntax error. Am i allowed to user viewbag as new List ? Commented Apr 24, 2014 at 10:23
  • 1
    What's the syntax error - I don't use ViewBag so not sure but perhaps you need to explicitly cast to SelectList i.e. (SelectList)ViewBag.WhBrandId? Commented Apr 24, 2014 at 10:27
  • Thank, casting (SelectList) solved . Commented Apr 24, 2014 at 10:35

1 Answer 1

4

First i suggest you edit your controller like this:

    public ActionResult Create()
    {
        ViewBag.SelectList = new List<SelectListItem>
        {
            new SelectListItem{Text = "Text",Value = "Value"},
            new SelectListItem{Text = "Text",Value = "Value"},
            new SelectListItem{Text = "Text",Value = "Value"}
        };

        return View();
    }

After that edit your view to this:

@Html.DropDownListFor(x => x.WhItem.WhBrandId, (List<SelectListItem>)ViewBag.SelectList)
Sign up to request clarification or add additional context in comments.

1 Comment

I want to get new List<SelectListItem> , from my controller within a viewbag. Could you please edit your code ? I am getting error when i want to use viewbag.

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.