0

I am still struggling with this--

All I am after is an order form, initially it will be a laptop order form but it might be a computer, printer etc in the future. So I have an Order Controller with a Laptop Action which makes a new laptop model. In the model I want to collect various information such as Customer Name, Customer Details etc. In my MODEL I also want to keep a Select List but I've been trying for ages and just cant seem to get it running. My laptop model has this:

--Laptop Model Select List

public SelectList screenSize = new SelectList(new[]
    {
        new SelectListItem { Text = "11.6", Value = "11.6" },
        new SelectListItem { Text = "15.6", Value = "15.6" },
        new SelectListItem { Text = "17", Value = "17" }
    }, "Value", "Text");

In the controller I accept the laptop on the post

[HttpPost]
    public ActionResult Index(Laptop laptopToEmail)
    {
        if (ModelState.IsValid)
        {
           ...send an email
        }
        else return View(laptopToEmail)
    }

In the view i am able to render out the list of items and I have a select list but I dont get a value passed to the email when i use

laptopToEmail.screenSize.SelectedValue

The view has this helper.

<%: Html.DropDownList("screenSize",Model.screenSize) %>

Am i missing something here? Surely it cant be this difficult to get a select list to work without a database in MVC.

1 Answer 1

1

You know, all these neato MVC Html helpers do have HTML equivalents. For a 2-item drop down, why not just write a bit of html:

<select>
   <option value="a">Option A</option>
   <option value="b">Option b</option>
</select>
Sign up to request clarification or add additional context in comments.

2 Comments

That would work if I have say a public string in my model and then a put in the select list in HTML with the name tag to match my model property but surely that dirtys MVC as the view has the select list not the model? in my controller i could then use if(ModelState.IsValid) to handle the HttpPost and it would get the value from the HTML select list - I've just tested it and it works but I'm not sure if that is good SOC
No law says you need the same model going to the view that comes back . . .

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.