1

I'm new to MVC and C# and having hard time with a dropdown list.

What I'm trying to accomplish is to initialize my page with an object that keeps the settings.
(I read settings from XML file).

Here's what I have

public class StoreSettings  
{  
    public String BackSrs2Path { get; set; }  
    public int NoLines { get; set; }  
    public String Requesturl { get; set; }  
}  

public class Store  
{  
    public String StoreId { get; set; }  
    public String Address { get; set; }  
    public StoreSettings StoreSettings { get; set; }  
}  

and the Model for my view page is a list of Store

@model System.Collections.Generic.List<Control2.Models.Store>  
@{     
List<SelectListItem> selectList = new List<SelectListItem>();  
foreach (var Store in Model)  
{  
SelectListItem i = new SelectListItem();  
i.Text = Store.StoreId;  
i.Value = Store.StoreId;  
selectList.Add(i);  
}  
 }  
 @using (Html.BeginForm())  
{  
 SelectList list = new SelectList(selectList, "Value", "Text");  
 @Html.DropDownList("ddl", list, "select store", new { onchange = "this.form.submit();" });  
  }  
  } 

By reading examples here managed to populate the dropdownlist from my model and postsback
but now i need to get only the selected object from the list and apply his seetings to the page to display it etc a message "you ve selected Store"+Storeid(the slected from dropdown)

Also this code is written in my cshtml page which isn't the best but couldn't figure how should I do it with ViewModel and dropdown list

1 Answer 1

2

Yes when I first started looking at the DropDownList Binding mechanisms of MVC I too had problems. What I'd like to do is suggest to you the following:

Create a Viewmodel and bind the entire view to the viewmodel... as follows:

public class VMStore
{
    public VMStore()
    {
        ItemsInDropDown = new List<SelectListItem>(){
            new SelectListItem{ Text="SomeValue", Selected=false, Value="SomeUniqueId"}
        };

    }
        public String StoreId { get; set; }
        public String Address { get; set; }
        public StoreSettings StoreSettings { get; set; }
        public string SelectedValue { get; set; }
        public IEnumerable<SelectListItem> ItemsInDropDown { get; set; }
        public void Post()
        {
           //This method will have the user selected value...

        }

}

The view will bind fields like this:

    @model WebApplication1.ViewModels.VMStore

    @{
        ViewBag.Title = "GetStore";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h2>GetStore</h2>

    <div>
        <h4>VMStore</h4>
        <hr />
        <dl class="dl-horizontal">

            @Html.BeginForm(){
            <dt>Select Store</dt>

            <dd>@Html.DropDownListFor(p=>Model.SelectedValue, Model.ItemsInDropDown) </dd>

            <dd><button type="submit">Submit</button></dd>
            }        

        </dl>
    </div>

The action methods will look like this:

    public ActionResult GetStore()
    {
        return View();
    }
    [HttpPost]
    public ActionResult GetStore(ViewModels.VMStore userdata) {
        if (ModelState.IsValid)
        {
            userdata.Post();
        }
        return View(userdata);

    }
Sign up to request clarification or add additional context in comments.

1 Comment

This is known as strong typed binding, it makes it really easy for you because you set up everything in the view model. On post back the entire ViewModel is passed back as the input parameter. Simply call the post method and you have access to what the user selected.

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.